Click here to Skip to main content
15,896,111 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.Extensibility;
using NUnit.Core;
using System.Diagnostics;
using NUnit.Framework;


namespace NinjaCross.Classes.Nunit.TestPerformance
{
    [TestFixture]
    public sealed class NJC_TestPerformanceAddInTest : NJC_TestPerformanceBaseTest
    {
        //----------------------------------------------------------------------------
        #region IAddin Members
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        [Test]
        public void Allocation()
        {
            GetNewInstance();
        }

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentNullException))]
        public void Install_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.Install(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void Install_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.Install(new CoreExtensions());
        }

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

        //----------------------------------------------------------------------------
        #region EventListener Members: Invalid parameters
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentException))]
        public void TestFinished_NotNull_EmptyFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "";
            addin.TestFinished(new TestResult(tn));
        }

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentException))]
        public void TestFinished_NotNull_NullFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = null;
            addin.TestFinished(new TestResult(tn));
        }

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentOutOfRangeException))]
        public void TestFinished_NotNull_MalformedFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "TestFullNameWithoutDot";
            addin.TestFinished(new TestResult(tn));
        }


        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentException))]
        public void TestStarted_NotNull_EmptyFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "";
            addin.TestStarted(tn);
        }

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentException))]
        public void TestStarted_NotNull_NullFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = null;
            addin.TestStarted(tn);
        }

        //----------------------------------------------------------------------------
        [Test]
        [ExpectedException(ExceptionType = typeof(ArgumentOutOfRangeException))]
        public void TestStarted_NotNull_MalformedFullName()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "TestFullNameWithoutDot";
            addin.TestStarted(tn);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunFinished_Exception_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunFinished((Exception)null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunFinished_TestResult_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunFinished((TestResult)null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunStarted_Null_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunStarted(null, default(int));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void SuiteFinished_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.SuiteFinished(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void SuiteStarted_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.SuiteStarted(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestFinished_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.TestFinished(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestOutput_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.TestOutput(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestStarted_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.TestStarted(null);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void UnhandledException_Null()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.UnhandledException(null);
        }

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

        //----------------------------------------------------------------------------
        #region EventListener Members: Valid parameters
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        [Test]
        public void RunFinished_Exception_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunFinished(new Exception("Test Exception"));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunFinished_TestResult_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunFinished(new TestResult(new TestName()));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunStarted_NotNull_NotNull_NoAssembliesLoaded()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.RunStarted("Test name", 1);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void RunStarted_NotNull_NotNull_AssembliesLoaded()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            String assemblyCodeBase = GetType().Assembly.CodeBase;
            String assemblyName = NJC_TestPerformanceUtils.GetFileNameFromCodeBase(assemblyCodeBase);
            Assert.IsNull(addin.GetCachedAssemblyByFileName(assemblyName));
            Assert.IsNull(addin.GetCachedAssemblyByCodeBase(assemblyCodeBase));
            addin.RunStarted(assemblyName, 1);
            Assert.IsNotNull(addin.GetCachedAssemblyByFileName(assemblyName));
            Assert.IsNotNull(addin.GetCachedAssemblyByCodeBase(assemblyCodeBase));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void SuiteFinished_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.SuiteFinished(new TestResult(new TestName()));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void SuiteStarted_NotNull_NoAssembliesLoaded()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.SuiteStarted(new TestName());
        }

        //----------------------------------------------------------------------------
        [Test]
        public void SuiteStarted_NotNull_NotNull_AssembliesLoaded()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            String assemblyCodeBase = GetType().Assembly.CodeBase;
            String assemblyName = NJC_TestPerformanceUtils.GetFileNameFromCodeBase(assemblyCodeBase);
            Assert.IsNull(addin.GetCachedAssemblyByFileName(assemblyName));
            Assert.IsNull(addin.GetCachedAssemblyByCodeBase(assemblyCodeBase));
            TestName tn = new TestName();
            tn.FullName = assemblyName;
            addin.SuiteStarted(tn);
            Assert.IsNotNull(addin.GetCachedAssemblyByFileName(assemblyName));
            Assert.IsNotNull(addin.GetCachedAssemblyByCodeBase(assemblyCodeBase));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestFinished_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "ClassName.TestName";
            addin.TestFinished(new TestResult(tn));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestOutput_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.TestOutput(new TestOutput("text", TestOutputType.Log));
        }

        //----------------------------------------------------------------------------
        [Test]
        public void TestStarted_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            TestName tn = new TestName();
            tn.FullName = "ClassName.TestName";
            addin.TestStarted(tn);
        }

        //----------------------------------------------------------------------------
        [Test]
        public void UnhandledException_NotNull()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            addin.UnhandledException(new Exception("test exception"));
        }

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

        //----------------------------------------------------------------------------
        #region Complex checks
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        [Test]
        public void Test_NoActionsExecuted()
        {
            NJC_TestPerformanceAddIn addin = GetNewInstance();
            String assemblyCodeBase = GetType().Assembly.CodeBase;
            String assemblyName = NJC_TestPerformanceUtils.GetFileNameFromCodeBase(assemblyCodeBase);
            addin.RunStarted(assemblyName, 1);
            TestName tn = new TestName();
            tn.FullName = GetType().FullName + ".Test_NoActionsExecuted";
            addin.SuiteStarted(tn);
            addin.TestStarted(tn);
            TestResult tr = new TestResult(tn);
            addin.TestFinished(tr);
            addin.SuiteFinished(tr);
            addin.RunFinished(tr);
        }

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

        //----------------------------------------------------------------------------
        #region utils methods
        //----------------------------------------------------------------------------

        //----------------------------------------------------------------------------
        private static NJC_TestPerformanceAddIn GetNewInstance()
        {
            return new NJC_TestPerformanceAddIn();
        }
        //----------------------------------------------------------------------------
        #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