January 25, 2015

MS-Test Unit Test Template

Here is a template class for an MS-Test unit test class. Pay careful attention to the order of the various test method calls.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject
{
    [TestClass]
    public sealed class VSUnitTestTemplate
    {
        // Invoked after AssemblyInitialize
        public VSUnitTestTemplate()
        {

        }

        // Invoked before all tests in an assembly have run
        [AssemblyInitialize]
        public static void AssemblyInitialize(TestContext testContext)
        {
            
        }

        // Invoked after all tests in an assembly have run
        [AssemblyCleanup]
        public static void AssemblyCleanup()
        {

        }

        // Executed before running any tests (but after the constructor)
        [ClassInitialize()]
        public static void ClassInitialize(TestContext context) 
        {

        }

        // Executed after running all the class tests
        [ClassCleanup()]
        public static void ClassCleanup() 
        {

        }

        // Executed before each individual Test
        [TestInitialize]
        public void TestInitialize() 
        {

        }

        // Executed after each individual Test
        [TestCleanup]
        public void TestCleanup() 
        {

        }
   
        // A single test
        [TestMethod]
        public void MyUnitTest()
        {
            // Setup

            // Perform test steps

            // Verify the results
            
        }
    }
}
From what I have seen in various blogs (see here and MS-Test versus NUnit here) most people recommend sticking to NUnit or some other open-source framework. The reasons are that MS-Test has:
  • Dependence on a test Metadata file.
  • A framework that in itself is quite slow.
  • A dependence on Visual Studio being installed for continous integration testing whereas something like NUnit only needs the NUnit assemblies installed.

No comments: