我们如何在 MSTest 中运行具有多个参数的测试方法?
NUnit 有一个名为 Values 的功能,如下所示:
[Test]
public void MyTest(
[Values(1,2,3)] int x,
[Values("A","B")] string s)
{
// ...
}
这意味着测试方法将运行六次:
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
我们现在使用 MSTest,但是是否有任何等效项,以便我可以使用多个参数运行相同的测试?
[TestMethod]
public void Mytest()
{
// ...
}
NUnit has a feature called Values, like below:
[Test]
public void MyTest(
[Values(1,2,3)] int x,
[Values("A","B")] string s)
{
// ...
}
This means that the test method will run six times:
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
We're using MSTest now, but is there any equivalent for this so that I can run the same test with multiple parameters?
[TestMethod]
public void Mytest()
{
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
编辑 4:看起来这是在 2016 年 6 月 17 日的 MSTest V2 中完成的:https://blogs.msdn.microsoft .com/visualstudioalm/2016/06/17/take-the-mstest-framework-forward-with-mstest-v2/
原始答案:
截至大约一周前,在 Visual Studio 2012 Update 1 中,现在可以实现类似的操作:
编辑:看来这仅在 WinRT/Metro 的单元测试项目中可用。糟糕的
编辑 2:以下是在 Visual Studio 中使用“转到定义”找到的元数据:
编辑 3:此问题是在 Visual Studio 的 UserVoice 论坛中提出的。
最后更新指出:
https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/3865310-allow-use-of-datatestmethod-datarow-in-all-unit
EDIT 4: Looks like this is completed in MSTest V2 June 17, 2016: https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/
Original Answer:
As of about a week ago in Visual Studio 2012 Update 1 something similar is now possible:
EDIT: It appears this is only available within the unit testing project for WinRT/Metro. Bummer
EDIT 2: The following is the metadata found using "Go To Definition" within Visual Studio:
EDIT 3: This issue was brought up in Visual Studio's UserVoice forums.
Last Update states:
https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/3865310-allow-use-of-datatestmethod-datarow-in-all-unit
此功能位于 现已预发布,可与 Visual Studio 2015 配合使用。
例如:
This feature is in pre-release now and works with Visual Studio 2015.
For example:
不幸的是,旧版本的 MSTest 不支持它。显然有一个 可扩展性模型,您可以自己实现。另一种选择是使用 数据驱动的测试。
我个人的意见是坚持使用 NUnit...
从 Visual Studio 2012 更新 1 开始,MSTest 具有类似的功能。请参阅McAden 的回答。
It is unfortunately not supported in older versions of MSTest. Apparently there is an extensibility model and you can implement it yourself. Another option would be to use data-driven tests.
My personal opinion would be to just stick with NUnit though...
As of Visual Studio 2012, update 1, MSTest has a similar feature. See McAden's answer.
与 NUnit 的
Value
(或TestCase
)属性不完全相同,但 MSTest 具有DataSource
属性,它允许您执行类似的操作。您可以将其连接到数据库或 XML 文件 - 它不像 NUnit 的功能那么简单,但它可以完成工作。
Not exactly the same as NUnit's
Value
(orTestCase
) attributes, but MSTest has theDataSource
attribute, which allows you to do a similar thing.You can hook it up to database or XML file - it is not as straightforward as NUnit's feature, but it does the job.
MSTest 有一个强大的属性,称为 DataSource。使用它,您可以按照您的要求执行数据驱动的测试。您可以将测试数据保存在 XML、CSV 或数据库中。以下是一些可以指导您的链接
MSTest has a powerful attribute called DataSource. Using this you can perform data-driven tests as you asked. You can have your test data in XML, CSV, or in a database. Here are few links that will guide you
实现起来非常简单 - 您应该使用
TestContext
属性和TestPropertyAttribute
。示例
编辑:
我准备了一些扩展方法来简化对 TestContext 属性的访问,并且就像我们有几个测试用例一样。请参阅此处处理简单测试属性的示例:
以及创建复杂测试对象的示例:
查看扩展 方法 和示例了解更多详细信息。
It's very simple to implement - you should use
TestContext
property andTestPropertyAttribute
.Example
EDIT:
I prepared few extension methods to simplify access to the
TestContext
property and act like we have several test cases. See example with processing simple test properties here:and example with creating complex test objects:
Take a look to the extension methods and set of samples for more details.
我无法让
DataRowAttribute
在 Visual Studio 2015 中工作,这就是我最终得到的结果:这里真正的解决方案是仅使用 NUnit(除非您像我一样陷入 MSTest 中)在这个特定的例子中)。
I couldn't get The
DataRowAttribute
to work in Visual Studio 2015, and this is what I ended up with:The real solution here is to just use NUnit (unless you're stuck in MSTest like I am in this particular instance).
当然,还有另一种方法可以做到这一点,但本线程中尚未讨论,即通过继承包含 TestMethod 的类。在下面的示例中,仅定义了一个 TestMethod,但创建了两个测试用例。
在 Visual Studio 2012 中,它在 TestExplorer 中创建两个测试:
DemoTest_A12_B4.test
There is, of course, another way to do this which has not been discussed in this thread, i.e. by way of inheritance of the class containing the TestMethod. In the following example, only one TestMethod has been defined but two test cases have been made.
In Visual Studio 2012, it creates two tests in the TestExplorer:
DemoTest_A12_B4.test
MSTest 不支持该功能,但您可以实现自己的属性来实现该功能。
查看在 MSTest 中启用参数化测试使用 PostSharp。
MSTest does not support that feature, but you can implement your own attribute to achieve that.
Have a look at Enabling parameterized tests in MSTest using PostSharp.
OP 示例是关于 NUnit 功能的,该功能可以轻松地获得所提供值的笛卡尔积。据我所知,这里没有答案涵盖该部分。我认为这是一个小小的挑战,并最终实现了以下实施。
[编辑:基于数组的重构 + Zip 值]
我对原始的基于枚举器的版本进行了一些重构(请参阅帖子历史记录),现在仅使用数组和循环索引。我还借此机会添加了新的 Zip 类型的值,该值将与笛卡尔生成创建的每个集合匹配不同的值。例如,这对于添加 ExpectedResult 可能很有用。
它仍然没有真正优化,所以请随意提出改进建议。
用法:
The OP example was about an NUnit feature that easily allows to have a cartesian product of provided values. As far as I was able to tell, no answer here covered that part. I saw this as a little challenge and ended up with the following implementation.
[Edit: Array based refactoring + Zip values]
I did some refactoring to the original Enumerator based version (see post history) to now use only Arrays and loop throuch indices instead. I also took the opporunity to add a new Zip type of values that will match a different value to every set created by the cartesian produce. This may be useful to add an ExpectedResult for instance.
It is still not really optimized so feel free to suggest improvements.
Usage:
这是
NUnits
[Combinatorial]
、[Sequential]
和[Values]
的重新实现。与默认情况下采用 [Combinatorial] 的
NUnit
不同,在MSTest
中,我们必须始终指定我们想要的,否则[Values ]
属性不会有任何效果。用法:
代码:
Here is a reimplentation of
NUnits
[Combinatorial]
,[Sequential]
and[Values]
.Unlike
NUnit
, which assumes[Combinatorial]
by default, inMSTest
we must always specify which one we want, else the[Values]
attribute will not have any effect.Usage:
Code: