Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the MSTest TestContext

Using MSTest, I needed to obtain the name of the current test from within the [TestInitialize] method. You can get this from the TestContext.TestName property.

I found an unexpected difference in behaviour between a static TestContext that is passed in to the [ClassInitialize] method and one that is declared as a public property (and gets set by the test runner).

Consider the following code:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestContext.Tests
{
    [TestClass]
    public class UnitTest1
    {
        public TestContext TestContext { get; set; }

        private static TestContext _testContext;

        [ClassInitialize]
        public static void SetupTests(TestContext testContext)
        {
            _testContext = testContext;
        }

        [TestInitialize]
        public void SetupTest()
        {
            Console.WriteLine(
                "TestContext.TestName='{0}'  static _testContext.TestName='{1}'",
                TestContext.TestName,
                _testContext.TestName);
        }

        [TestMethod] public void TestMethod1() { Assert.IsTrue(true); }

        [TestMethod] public void TestMethod2() { Assert.IsTrue(true); }

        [TestMethod] public void TestMethod3() { Assert.IsTrue(true); }
    }
}

This causes the following to be output (copy-pasted from the Resharper test runner output in VS2013):

TestContext.TestName='TestMethod1'  static _testContext.TestName='TestMethod1'
TestContext.TestName='TestMethod2'  static _testContext.TestName='TestMethod1'
TestContext.TestName='TestMethod3'  static _testContext.TestName='TestMethod1'

I had previously assumed that the two instances of TestContext would be equivalent, but clearly they're not.

  • The public TestContext property behaves as I expect
  • The private static TestContext value that gets passed to the [ClassInitialize] method does not. Since TestContext has properties that relate to the currently running test, this implementation seems misleading and broken

Is there any scenario where you would actually prefer to use the TestContext passed to the [ClassInitialize] method, or it is best ignored and never used?

like image 925
Richard Ev Avatar asked Jun 16 '14 17:06

Richard Ev


People also ask

What is Testcontext MSTest?

Used to store information that is provided to unit tests.

Is MSTest a testing framework?

MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE. It is also referred to as the Unit Testing Framework. However, MSTest is the same within the developer community. MSTest is used to run tests.

Which is better NUnit or MSTest?

The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.


3 Answers

As [ClassInitialize] is only called at the beginning, the test name is TestMethod1. This is stale after the first test run.

TestContext is set for every method, and thus has the current test name.

Yes, it is a bit silly.

like image 153
BanksySan Avatar answered Oct 18 '22 19:10

BanksySan


The method

[ClassInitialize]
public static void SetupTests(TestContext testContext) { }

is called before the property set TestContext is set. So if you need the context in SetupTests then the parameter is usefull. Otherwise use the TestContext property, which is set before each

[TestInitialize]
public void SetupTest() { }
like image 6
Peter Avatar answered Oct 18 '22 20:10

Peter


If you want to pass your objects created in method [ClassInitialize] (or[AssemblyInitialize]) to the cleanup methods and your tests, you must keep its initialization context in a separate static variable, aside from the regular TestContext. Only this way can you retrieve it later in your code.

public TestContext TestContext { get; set; } // regular test context
private static TestContext ClassTestContext { get; set; } // global class test context

[ClassInitialize]
public static void ClassInit(TestContext context)
{
        ClassTestContext = context;
        context.Properties["myobj"] = <Some Class Level Object>;
}

[ClassCleanup]
public static void ClassCleanup()
{
    object myobj = (object)ClassTestContext.Properties["myobj"];
}

[TestMethod]
public void Test()
{
    string testname = (string)TestContext.Properties["TestName"] // object from regular context
    object myobj = (object)ClassTestContext.Properties["myobj"]; // object from global class context
}

MSTest framework does not preserve the context objects passed to [ClassInitialize]/[AssemblyInitialize] method, so after the return they will be lost forever unless you explicitly save them.

like image 1
Alex Avatar answered Oct 18 '22 19:10

Alex