找出要在MS TestInitialize中执行的下一个测试方法

maj*_*tor 8 c# unit-testing vs-unit-testing-framework

我将特定测试方法的测试数据保存在与函数相同的文件夹中.我以前在每个函数调用中都有相同的函数调用[TestMethod],ClearAllAndLoadTestMethodData()它确定了方法名称StackTrace.现在,我将此功能移至[TestInitialize].如何找到即将执行的方法的名称?

我想TestContext提供这个.我可以通过[AssemblyInitialize()]首次运行访问它,它的属性Name设置为testmethod的名称.但是,稍后这不会改变(如果我将对象保存在静态字段中).

cha*_*sos 20

所有测试之前,AssemblyInitialize方法只执行一次.

使用方法TestContext内部TestInitialize:

[TestClass]
public class TestClass
{
    [TestInitialize]
    public void TestIntialize()
    {
        string testMethodName = TestContext.TestName;
    }

    [TestMethod]
    public void TestMethod()
    {
    }

    public TestContext TestContext { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢.我不知道我必须申报财产. (4认同)