MS Test V2 Introduction

MS Test V2 Introduction

Introduction

MS test is a unit testing automation framework which facilitates to write unit tests in .Net framework. It is the primary unit testing framework for Visual Studio. MS Test provides various elements to write the unit tests. The parameter test was one of the important feature that missed in MS Test V1 and MS Test(V2) supports the parameter tests along with custom data source, and other features. In this article, I will introduce the MS Test V2 to you and you will more reference links as you read along.

MS Test Archtecture

VSTest is the Visual Studio test runner which run all unit tests from the specific Test framework. Test Framework will facilitate to create your unit tests, in our case MS Test is the test framework. VSTest provides adapter based extension to communicate with test frameworks so that you can write unit tests from xUnit or NUnit or others.

Anatomy of MS Test V2

MSTest.TestFramework package contains the testing framework and its contract. All unit test related contracts available under Microsoft.VisualStudio.TestTools.UnitTesting namespace.

MS test uses attribute-based conventions to write unit tests as other frameworks do. Here is the list of important attributes,

  1. TestClassAttribute(must have) – a Class level attribute which marks a class to test executable class. Test runner will query all test cases based on this attribute, so it is mandatory to mark your unit test methods container class as test class otherwise it won’t execute in test runner.
  2. TestMethodAttribute(must have) – Method level attribute which marks a method to unit test and it is mandatory otherwise your unit test won’t execute in test runner.
  3. WorkItemAttribute(optional) – Method level attribute that helps us link one or more work item from TFS. It is an optional attribute.
  4. DataRowAttribute (optional) – Method level attribute which helps to inline data to a parameterized unit test. You can use this attribute to test a unit test with a set of inputs. It is an optional attribute.
    [DataRow(1, 1, 2)]
    [DataRow(0, 0, 0)]
    [DataRow(-1, -1, -2)]
    [TestMethod]
    public void Add(int a, int b,int result)
    {
        var sum = a + b;
        Assert.IsTrue(sum == result);
    }
  5. DyamicDataAttribute(optional) – Method level attribute for dynamic data for the parameterized test method. It helps us to generate test data from a method or property.
    [DataTestMethod]
    [DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
    public void AddDynamicDataMethod(int a, int b, int expected)
    {
        var actual = a + b;
        Assert.AreEqual(expected, actual);
    }
    
    private static IEnumerable<object[]> GetData()
    {
       return new[]
       {
           new object[] { 1, 1, 2},
           new object[] { 0, 0, 0 },
           new object[] { -1, 1, 0  },
           new object[] { -1, -1, -2 }
       };
    }
  6. ClassInitializeAttribute(optional) – Method level attribute for test class level test fixture setup method. This executes start of each test class run and it will execute once. You can use this method to initialize the test class level states or data. It is an optional attribute.
  7. ClassCleanupAttribute(optional) – Method level attribute for test class level test fixture teardown method. This method executes end of each test class run and it will execute once. Cleaning up the test class level states or data is the perfect use case for this method. It is an optional attribute.
  8. TestInitializeAttribute(optional) – Method level attribute for test level setup method. This method will execute before each unit tests in a test class and this is the perfect place to initialize all test level states or data. It is an optional attribute.
  9. TestCleanupAttribute(optional) – Method level attribute for test level teardown method. This method will execute after each unit tests. It is an optional attribute.
    [TestClass]
    public class TestClass
    {
        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            // Executes once before the test run. (Optional)
        }
    
        [ClassInitialize]
        public static void TestFixtureSetup(TestContext context)
        {
            // Executes once for the test class. (Optional)
        }
    
        [TestInitialize]
        public void Setup()
        {
            // Runs before each test. (Optional)
        }
    
        // Mark that this is a unit test method. (Required)
        [TestMethod]
        public void TestMethod()
        {
            // Your test code goes here.
        }
    
    
        [AssemblyCleanup]
        public static void AssemblyCleanup()
        {
            // Executes once after the test run. (Optional)
        }
    
        [ClassCleanup]
        public static void TestFixtureTearDown()
        {
            // Runs once after all tests in this class are executed. (Optional)
            // Not guaranteed that it executes instantly after all tests from the class.
        }
    
        [TestCleanup]
        public void TearDown()
        {
            // Runs after each test. (Optional)
        }      
    }

Execution Flow of MS Test

  1. Loads all test assemblies (which has one or more test classes)
  2. Execute Assembly Initialize method for each assembly if method available
  3. Execute Class Initialize method for each test class if method available
  4. Execute Test Initialize method for each test if method available
  5. Execute the test method
  6. Execute Test Cleanup method for each test if method available
  7. Execute Class Cleanup method for each test class if method available
  8. Execute Assembly Cleanup method for each assembly if method available

Life Cycle

Test Assertion

MS test provides various test assertion methods out of the box and those are,

  1. Assert – Contains general-purpose assertion expressions. Refer to this link for more information.
  2. CollectionAssert – Contains specialized assertion expressions for collection. Refer to this link for more information.
  3. StringAssert – Contains specialized assertion expressions for a string. Refer to this link for more information.

Testing Exceptions

MS test provides a special attribute called ExpectedException to write unit tests for an exception. It is mandatory to write exception test case when SUT throws or forward any important exceptions that SUT cares about.

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void It_should_throw_invalid_operation_exception_for_invalid_user_id()
{
 
}

For more information on MS Test framework elements, refer to this link.

Installation of MS Test V2

MS Test V2 package included in the VS 2017 project template but for VS 2015 you must manually install required packages.

  1. Create a Unit Test project
  2. Install the following packages
    1. MSTest.TestFramework
    2. MSTest.TestAdapter
  3. Remove the existing VisualStudio.QualityTools.UnitTestFramework assembly reference
  4. Create your first unit class and then create your unit test methods.

Migration to MS Test V2:

For migration, you required to have VS 2015 or higher. Do the following steps to migrate your existing MS Test V1 to V2,

  1. Open the existing Unit Test project
  2. Install the following packages
    1. MSTest.TestFramework
    2. MSTest.TestAdapter
  3. Remove the existing VisualStudio.QualityTools.UnitTestFramework assembly reference.
  4. That’s it! Now, you can run all your unit tests in MS Test V2.

Leave a Reply

Your email address will not be published. Required fields are marked *