Visual Studio 2015 Intellitest 的参数化单元测试

Parameterized Unit Tests with Visual Studio 2015 Intellitest

我长期以来一直想在 MSTest 中看到的一个功能是参数化单元测试 (PUT)。我很高兴听到 Intellitest would be capable of creating said tests。然而,我已经开始使用 Intellitest,我认为我对 PUT 的定义与 Microsoft 的不同。

当我想"PUT"的时候,我想TestCases in NUnit, or Theories in xUnit. People much smarter than me seem to use the same terminology

有人可以告诉我 Intellitest 是否真的能够以与 NUnit 或 xUnit 相同的方式创建 PUT,或者这是一个重载术语的问题,在 Intellitest 中意味着一件事,而对于大多数其他测试框架来说又是另一回事?谢谢

Intellitest 生成的参数化单元测试与其他测试框架中常见的 PUT 不同

在 MSTest/Intellitest 世界中,PUT 用于 intelligently generate other unit tests

为了在 MSTest 中使用不同的数据集多次执行测试,我们仍然需要与 Data-Driven Unit Tests or use MSTestHacks as suggested in How to RowTest with MSTest? 搏斗。

参数化单元测试 (PUT) 是通过使用参数对单元测试进行的直接概括。 PUT 对一整套可能的输入值的代码行为进行陈述,而不仅仅是单个示例性输入值。在某种程度上,它类似于您提供的链接。不同之处在于生成数据以输入参数化单元测试时 - IntelliTest 可以自动为 PUT 生成输入数据。我请求您参考以下内容: http://blogs.msdn.com/b/visualstudioalm/archive/2015/07/05/intellitest-one-test-to-rule-them-all.aspx 上下文。

As of June 2016, this feature has been added to "MSTest V2", which can be installed via NuGet by adding the MSTest.TestAdapter and MSTest.TestFramework 个包:

Install-Package MSTest.TestAdapter
Install-Package MSTest.TestFramework

Be aware that these are different than the version of the test framework that ships with e.g. Visual Studio 2017. To use them, you'll likely need to remove the reference(s) to Microsoft.VisualStudio.QualityTools.UnitTestFramework.

一旦安装了这些,您就可以简单地使用 RowDataAttribute,如下例所示:

[TestMethod]
[DataRow(1, 1, 2)]
[DataRow(3, 3, 6)]
[DataRow(9, -4, 5)]
public void AdditionTest(int first, int second, int expected) {
  var sum = first+second;
  Assert.AreEqual<int>(expected, sum);
}

Obviously, you aren't restricted to int here. You can also use string, float, bool, or any other primitive value type.

这与 Windows Store App projects 之前可用的实现相同,如果您熟悉的话。