Understanding the MSTest TestContext

前端 未结 5 1366
一个人的身影
一个人的身影 2020-12-14 07:03

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

相关标签:
5条回答
  • 2020-12-14 07:16

    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)
  • 2020-12-14 07:25

    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() { }
    
    0 讨论(0)
  • 2020-12-14 07:30

    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.

    0 讨论(0)
  • 2020-12-14 07:31

    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()
            }
    
        }
    
    0 讨论(0)
  • 2020-12-14 07:35

    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.

    0 讨论(0)
提交回复
热议问题