51

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?

6
  • The runner is creating a new TestContext instance before each test. Are you asking why it was designed this way? Commented Jun 16, 2014 at 18:58
  • 2
    @mikez - To me the private static TestContext behaviour seems wrong. That's what I'm asking about.
    – Richard Ev
    Commented Jun 16, 2014 at 20:51
  • _testContext is a field which you assigned only once, inside the method marked with the [ClassInitialize] attribute. Why would you expect it to change between tests? As @mike wrote, each test gets a new TestContext instance.
    – vgru
    Commented Jun 17, 2014 at 6:39
  • like @mikez said, a new TestContext is created for each test. As you can see from the output, The ClassInitialize method gets just the context of the first test. More on this topic: blog.adilakhter.com/2008/05/04/more-on-unit-testing-testcontext
    – qbik
    Commented Jun 17, 2014 at 6:47
  • 1
    Static TestContext set for class level, non-static TestContext instantiated for each Unit Test. Commented Dec 5, 2018 at 18:52

5 Answers 5

39

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.

3
  • 2
    My expectation/hope was that the test runner would maintain a reference to the TextContext that it passes to the [ClassInitialize] method, and keep its values correctly updated...
    – Richard Ev
    Commented Jun 17, 2014 at 14:41
  • 2
    @RichardEverett That would have been more practical and more intuitive. Better still, would have been to pass the Initialize() method only the static data.
    – BanksySan
    Commented Jun 17, 2014 at 14:44
  • My public TestContext Context { get; set; } property's setter is never invoked so I never get a non-null reference to the current (non-static) TestContext. I'm unsure if I'm missing something...
    – Dai
    Commented Jan 28 at 20:47
6

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

Scenario: context for each test.

Applies to Visual Studio 2017 with the following libraries:

  • Microsoft.VisualStudio.TestPlatform.TestFramework
  • Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions

Sample code:

    [TestClass]
    public class MyTestClass
    {

        public TestContext TestContext { get; set; }

        /// <summary>
        /// Run before each UnitTest to provide additional contextual information.
        /// TestContext reinitialized before each test, no need to clean up after each test.
        /// </summary>
        [TestInitialize]
        public void SetupTest()
        {
            TestContext.Properties.Add("MyKey", "My value ...");

            switch (TestContext.TestName)
            {
                case "MyTestMethod2":
                    TestContext.Properties["MyKey2"] = "My value 2 ...";
                    break;
            }

        }

        [TestMethod]
        public void MyTestMethod()
        {
            // Usage:
            // TestContext.Properties["MyKey"].ToString()
        }   

        [TestMethod]
        public void MyTestMethod2()
        {
            // Usage:
            // TestContext.Properties["MyKey"].ToString()

            // also has:
            // TestContext.Properties["MyKey2"].ToString()
        }

    }
1
  • 1
    Instead of using that switch block, it's better to use TestPropertyAttribute on a test method to set the custom property MyKey2 into the test context. The property will be available for that test method and also will be set in TextContext when SetupTest method is called.
    – SalgoMato
    Commented Jul 3, 2020 at 11:45
1

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.

0
0

Issue can be resolved like this:

First define a static property:

    private static string _targetUrl;

Then assing the value from runsetting file inside the ClassInitialize type of method, Use TestContext as an input parameter.

[ClassInitialize]
    public static void Initialize(TestContext testContext)
    {
        _targetUrl = testContext.Properties["targetUrl"].ToString();           
    }

Variable is initialized, ready to use it further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.