Tag: testcontext

了解MSTest TestContext

使用MSTest,我需要从[TestInitialize]方法中获取当前测试的名称。 您可以从TestContext.TestName属性获取此信息。 我发现传入[ClassInitialize]方法的静态TestContext与声明为公共属性的静态TestContext (由测试运行器设置)之间存在意外差异。 请考虑以下代码: 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( “TextContext.TestName='{0}’ static _testContext.TestName='{1}'”, TestContext.TestName, _testContext.TestName); } [TestMethod] public void TestMethod1() […]