代码之家  ›  专栏  ›  技术社区  ›  LukáÅ¡ Lánský

DataTestMethod与TestMethod

  •  27
  • LukáÅ¡ Lánský  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  16
  •   Nkosi    5 年前

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

    参考:

    Taking the MSTest Framework forward with “MSTest V2”

    Github: Unit Test Samples

        2
  •  13
  •   Nathan    5 年前

    不需要Hi@cactualoid数据测试方法。 继续,将TestMethod与DataRows一起使用,以数据驱动测试。 如有任何疑问,请参阅以下官方文件: https://github.com/microsoft/testfx-docs

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

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

    因此,根据微软的说法,与DataTestMethod相比,它更倾向于使用TestMethod。