c# - VS 团队测试 : Multiple Test Initialize Methods in Test Class

标签 c# .net visual-studio-2010 unit-testing

我在 TeamTest 中有一个名为“MyClassTest”的单元测试项目。该项目具有三个 TestMethods。每个方法都需要自己的测试初始化​​步骤。但是当我将 TestInitializeAttribute 应用于三个初始化方法时,它说该属性不应多次使用。那么在 Visual Studio Team Test 中初始化每个测试方法的属性应该是什么?

引用:

  1. VS Team Test: .Net Unit Testing with Excel as Data Source: Adapter Failed

  2. How to create Startup and Cleanup script for Visual Studio Test Project?

  3. VS 2010 Load Tests Results with custom counters

  4. How to log unit test entry and leave in MSTest

  5. Can a unit test project load the target application's app.config file?

最佳答案

根据 MSDN TestInitializeAttribute:

  • 不能多次使用 (AllowMultiple = false),并且
  • 不能被继承来创建你自己的TestInitializeAttribute

因此,我的建议是创建不带 TestInitialize 属性的测试初始化​​方法。然后在独特的 TestInitialize 方法中检查哪个是当前执行的 TestMethod 并调用适当的初始化方法:

[TestClass]
public class UnitTest
{
    public TestContext TestContext { get; set; }

    [TestInitialize]
    public void Initialize()
    {
        switch (TestContext.TestName)
        {
            case "TestMethod1":
                this.IntializeTestMethod1();
                break;
            case "TestMethod2":
                this.IntializeTestMethod2();
                break;
            default:
                break;
        }
    }

    [TestMethod]
    public void TestMethod1()
    {
    }

    [TestMethod]
    public void TestMethod2()
    {
    }

    public void IntializeTestMethod1()
    {
        //Initialize Test Method 1
    }

    public void IntializeTestMethod2()
    {
        //Initialize Test Method 2
    }
}

关于c# - VS 团队测试 : Multiple Test Initialize Methods in Test Class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10699795/

相关文章:

c# - NetMQ 套接字处理后无法重用端点

c# - 使用 System.Reflection 获取方法的全名

visual-studio-2010 - 如何防止 “There appears to be a discrepancy between the solution'的源代码控制……”而无需更改.sln文件

c# - Windows 10 WebView InvokeScript 不工作

.net - TPL数据流: creating a custom splitblock

visual-studio-2010 - TFS2010 工作流事件 "InvokeProcess": How to display the process on the agent instead of running it in background?

c# - 访问 Datagridview (WinForms) 中的不可见列

c# - 如何获取颁发者证书的指纹或公钥?

c# - GetNavigationState 不支持传递给 Frame.Navigate 的参数类型的序列化

c# - 从 WebBrowserControl 中单击的 HtmlElement 获取 XPath