Understanding the MSTest TestContext

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.


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.


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() { }