DataTestMethod与TestMethod

时间:2017-10-24 07:48:14

标签: c# unit-testing mstest

我开始使用MSTest 2 DataRow属性来检查单个测试中的多个案例:

[TestMethod]
[DataRow(1, 1, 2)]
[DataRow(1, 2, 3)]
public void AdditionWorks(int op1, int op2, int expectedResult)
{
    Assert.AreEqual(expectedResult, new Sut().Add(op1, op2));
}

在NCrunch和CI中都很好用。只是现在我注意到有一个特殊属性DataTestMethod应该标记这样的测试,而不是TestMethod

有区别吗?有理由特别使用一种变体吗?

2 个答案:

答案 0 :(得分:11)

两个属性都有效,因为在与先前版本的MSTest相同的命名空间中定义了相同的属性。这样做是为了向后兼容。

但建议对于数据驱动的测试,在较新版本的MSTest中使用DataTestMethod

参考:

Taking the MSTest Framework forward with “MSTest V2”

Github: Unit Test Samples

答案 1 :(得分:1)

ShreyasRmsft在GitHub上评论了以下内容:

  

不需要@cactuaroid DataTestMethod。   继续并将TestMethod与DataRows结合使用以数据驱动测试。   如有任何疑问,请关注https://github.com/microsoft/testfx-docs

上的官方文档

https://github.com/microsoft/testfx/issues/614

https://github.com/microsoft/testfx-docs/issues/64

因此,根据Microsoft的说法,首选使用TestMethod而不是DataTestMethod。