January 25, 2015

MS-Test Class 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.
[TestClass]
public sealed class VSUnitTestTemplate
{
    // Invoked after AssemblyInitialize
    public VSUnitTestTemplate()
    {
    }

#region Setup/TearDown 
    // 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() 
    {

    }
#endregion Setup/TearDown        

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

        // act: Perform test steps

        // assert: Verify the results
        
    }
    
    // You can add test parameters to a test and use it to test multiple cases 
   [TestMethod]
   [DataRow(5.0d, 0.0d, 3)]
   [DataRow(5.0d, 1.0d, 3)]
   [DataRow(5.0d, 0.0d, 5)]
   public void SomeTest(
     double fullLengthSecs, double startTimeSecs, int expectedValue)
   {
   }
}
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: