Click here to Skip to main content
15,897,187 members
Articles / Web Development / XHTML

Performance Benchmarks using NUnit Addin

Rate me:
Please Sign up or sign in to vote.
4.97/5 (16 votes)
27 Oct 2012LGPL312 min read 90.8K   733   74  
This tutorials introduces a new NUnit Addin able to record execution time of unit tests and generate XML, CSV, HTML performances reports with charts and history tracking.
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Core;
using NUnit.Framework;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Diagnostics;


namespace NinjaCross.Classes.Nunit.TestPerformance
{
    /// <summary>
    /// This attribute must be applied to the NUNIT test methods that needs for performances recording
    /// </summary>
    [TestFixture]
    public sealed class NJC_TestPerformanceRecorderAttributeTest : NJC_TestPerformanceBaseTest
    {
        //----------------------------------------------------------------------------
        #region Misc and properties
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        [Test]
        public void Allocation()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.IsNull(attribute.TestName);
            Assert.IsNull(attribute.TestDescription);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void Allocation_Null_Null()
        {
            NJC_TestPerformanceRecorderAttribute attribute = new NJC_TestPerformanceRecorderAttribute(null, null);
            Assert.IsNull(attribute.TestName);
            Assert.IsNull(attribute.TestDescription);
        }


        //----------------------------------------------------------------------------
        [Test]
        public void Allocation_NotNull_Null()
        {
            String name = "Test name";
            NJC_TestPerformanceRecorderAttribute attribute = new NJC_TestPerformanceRecorderAttribute(name, null);
            Assert.AreEqual(name, attribute.TestName);
            Assert.IsNull(attribute.TestDescription);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void Allocation_Null_NotNull()
        {
            String description = "Test description";
            NJC_TestPerformanceRecorderAttribute attribute = new NJC_TestPerformanceRecorderAttribute(null, description);
            Assert.IsNull(attribute.TestName);
            Assert.AreEqual(description, attribute.TestDescription);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void Allocation_NotNull_NotNull()
        {
            String name = "Test name";
            String description = "Test description";
            NJC_TestPerformanceRecorderAttribute attribute = new NJC_TestPerformanceRecorderAttribute(name, description);
            Assert.AreEqual(name, attribute.TestName);
            Assert.AreEqual(description, attribute.TestDescription);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestName()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.IsNull(attribute.TestName);
            String name = "Test name";
            attribute.TestName = name;
            Assert.AreEqual(name, attribute.TestName);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestDescription()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.IsNull(attribute.TestDescription);
            String desc = "Test description";
            attribute.TestDescription = desc;
            Assert.AreEqual(desc, attribute.TestDescription);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void OutputTarget()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.IsNull(attribute.OutputTarget);
            String val = "new value";
            attribute.OutputTarget = val;
            Assert.AreEqual(val, attribute.OutputTarget);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void OutputTargetFormat()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.AreEqual(NJC_TestPerformanceTargetFormat.Xml, attribute.OutputTargetFormat);
            NJC_TestPerformanceTargetFormat val = NJC_TestPerformanceTargetFormat.Html;
            attribute.OutputTargetFormat = val;
            Assert.AreEqual(val, attribute.OutputTargetFormat);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void OutputTargetIdentificationFormat()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.AreEqual(NJC_TestPerformanceTargetIdentificationFormat.ClassFullNameAndMethodName, attribute.OutputTargetIdentificationFormat);
            NJC_TestPerformanceTargetIdentificationFormat val = NJC_TestPerformanceTargetIdentificationFormat.MethodName;
            attribute.OutputTargetIdentificationFormat = val;
            Assert.AreEqual(val, attribute.OutputTargetIdentificationFormat);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void OutputTargetKind()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.AreEqual(NJC_TestPerformanceTargetKind.FileSystem, attribute.OutputTargetKind);
            NJC_TestPerformanceTargetKind val = NJC_TestPerformanceTargetKind.FileSystem;
            attribute.OutputTargetKind = val;
            Assert.AreEqual(val, attribute.OutputTargetKind);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void OutputTargetWriteMode()
        {
            NJC_TestPerformanceRecorderAttribute attribute = GetNewInstance();
            Assert.AreEqual(NJC_TestPerformanceTargetWriteMode.Append, attribute.OutputTargetWriteMode);
            NJC_TestPerformanceTargetWriteMode val = NJC_TestPerformanceTargetWriteMode.Overwrite;
            attribute.OutputTargetWriteMode = val;
            Assert.AreEqual(val, attribute.OutputTargetWriteMode);
        }

        //----------------------------------------------------------------------------
        #endregion
        //----------------------------------------------------------------------------
       
        //----------------------------------------------------------------------------
        #region Utils methods
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        private static NJC_TestPerformanceRecorderAttribute GetNewInstance()
        {
            return new NJC_TestPerformanceRecorderAttribute();
        }

        //----------------------------------------------------------------------------
        #endregion
        //----------------------------------------------------------------------------

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Dogma Solutions
Italy Italy
I'm a senior developer in .NET, COM & Win32 and I work for an Italian company involved in e-learning and competency-analisys systems.
My work is also my main hobby, and I spend a lot of my spare time writing tutorials and codes for video games and multimedia-related arguments.

Comments and Discussions