DataTestMethod 与测试方法

DataTestMethod vs TestMethod

我开始使用 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.

有区别吗?特别使用一种变体的原因?

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

参考:

Taking the MSTest Framework forward with “MSTest V2”

Github: Unit Test Samples

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

Hi @cactuaroid DataTestMethod is not needed. Go ahead and use TestMethod with DataRows to data drive your tests. For any more doubts follow the official docs at https://github.com/microsoft/testfx-docs

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

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

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