【问题标题】:DataTestMethod vs TestMethod数据测试方法与测试方法
【发布时间】:2018-04-04 22:07:48
【问题描述】:

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

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

【问题讨论】:

    标签: c# unit-testing mstest


    【解决方案1】:

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

    参考:

    Taking the MSTest Framework forward with “MSTest V2”

    Github: Unit Test Samples

    【讨论】:

    • 我不明白向后兼容的说法:[DataRow] 是新的,所以如果它不能与[TestMethod] 一起使用,什么都不会丢失。
    • @LukášLánský,名称空间是向后兼容的。新框架中也有TestMethod 以及与前一个相同的命名空间。这样,当您删除旧的测试框架时,您所要做的就是添加新的[DataRow],其他一切仍然有效。 DataTestMethod 也派生自 TestMethod。测试引擎会因此识别数据测试。
    【解决方案2】:

    ShreyasRmsft 在 GitHub 上发表了以下评论:

    您好@cactuaroid DataTestMethod 不需要。 继续使用带有 DataRows 的 TestMethod 来驱动您的测试。 如有更多疑问,请关注https://github.com/microsoft/testfx-docs的官方文档@

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2011-01-24
      • 2021-10-28
      • 2018-11-07
      • 2014-01-07
      相关资源
      最近更新 更多