相关疑难解决方法(0)

了解MSTest TestContext

使用MSTest,我需要从[TestInitialize]方法中获取当前测试的名称.您可以从TestContext.TestName酒店获取此信息.

我发现TestContext传入[ClassInitialize]方法的静态与声明为公共属性的静态(并由测试运行器设置)之间的行为有意想不到的差异.

请考虑以下代码:

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); }
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# mstest testcontext

23
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1

mstest ×1

testcontext ×1