Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...