找出要在MS TestInitialize中执行的下一个测试方法
我将特定测试方法的测试数据保存在与函数相同的文件夹中。 我以前在每个[TestMethod]
, ClearAllAndLoadTestMethodData()
中都有相同的函数调用,它通过StackTrace
确定方法名称。 现在,我将此函数移至[TestInitialize]
。 如何找到即将执行的方法的名称?
我以为TestContext
提供了这个。 我可以通过[AssemblyInitialize()]
访问它,并在第一次运行时将其属性Name
设置为testmethod的名称。 但是,稍后这不会改变(如果我将对象保存在静态字段中)。
所有测试之前, AssemblyInitialize方法只执行一次。
在TestInitialize
方法中使用TestContext
:
[TestClass] public class TestClass { [TestInitialize] public void TestIntialize() { string testMethodName = TestContext.TestName; } [TestMethod] public void TestMethod() { } public TestContext TestContext { get; set; } }