问题描述
我发现NUnit中的TestCase
功能非常有用,可以快速指定测试参数,而无需为每个测试使用单独的方法. MSTest中有类似的东西吗?
I find the TestCase
feature in NUnit quite useful as a quick way to specify test parameters without needing a separate method for each test. Is there anything similar in MSTest?
[TestFixture]
public class StringFormatUtilsTest
{
[TestCase("tttt", "")]
[TestCase("", "")]
[TestCase("t3a4b5", "345")]
[TestCase("3&5*", "35")]
[TestCase("123", "123")]
public void StripNonNumeric(string before, string expected)
{
string actual = FormatUtils.StripNonNumeric(before);
Assert.AreEqual(expected, actual);
}
}
推荐答案
Microsoft最近宣布了"MSTest V2" (请参阅博客文章).这样一来,您就可以一致地(桌面,UWP等)使用DataRow
属性!
Microsoft recently announced "MSTest V2" (see blog-article). This allows you to consistently (desktop, UWP, ...) use the DataRow
-attribute!
[TestClass]
public class StringFormatUtilsTest
{
[DataTestMethod]
[DataRow("tttt", "")]
[DataRow("", "")]
[DataRow("t3a4b5", "345")]
[DataRow("3&5*", "35")]
[DataRow("123", "123")]
public void StripNonNumeric(string before, string expected)
{
string actual = FormatUtils.StripNonNumeric(before);
Assert.AreEqual(expected, actual);
}
}
再次,不幸的是,Visual Studio Express的测试资源管理器"无法识别这些测试.但是至少完整"的VS版本现在支持该功能!
Again, Visual Studio Express' Test Explorer unfortunately doesn't recognize these tests. But at least the "full" VS versions now support that feature!
要使用它,只需安装NuGet软件包 MSTest.TestFramework 和 MSTest.TestAdapter (目前均为预发行版本).
To use it, just install the NuGet packages MSTest.TestFramework and MSTest.TestAdapter (both pre-release as of now).
如果不必坚持使用MSTest,而只是使用它来通过Test Explorer运行测试,因为您只有Visual Studio Express版本,则可能是为您提供解决方案:
If don't have to stick with MSTest and you're just using it for being able to run the tests via Test Explorer , then this might be a solution for you:
VsTestAdapter VSIX扩展可以运行NUnit测试通过测试资源管理器.不幸的是,VS Express用户无法安装扩展...但是幸运的是, VsTestAdapter也附带了一个普通的NuGet-Package !
There's the VsTestAdapter VSIX extension for being able to run NUnit tests via Test Explorer. Unfortunately, VS Express users can't install extensions...But fortunately the VsTestAdapter comes with a plain NuGet-Package, too!
不幸的是,上述陈述是不正确的.尽管完全可以通过Express版本安装该软件包,但它无用,因为它无法利用Test Explorer.以前在TestAdapter的旧版本上有一个旁注,已被删除从 2.0.0的描述页面:
Unfortunately the aforementioned statement isn't true. While it's perfectly possible to install the package via an Express edition, it's useless, since it can't utilize the Test Explorer. There's previously been a side note on an older version of the TestAdapter, which was removed from the 2.0.0's description page:
这篇关于MSTest是否与NUnit的TestCase等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!