MSTEST中的DataTestMethod和DataRow属性 [英] DataTestMethod and DataRow attributes in MSTEST

查看:698
本文介绍了MSTEST中的DataTestMethod和DataRow属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Microsoft视频中看到了有关这些属性的Visual Studio Update 2.但是,我找不到关于它们的任何其他信息,也找不到与他们一起构建的项目.

有人知道这些属性或如何使它们起作用吗?

[DataTestMethod]
[DataRow("a", "b")]
[DataRow(" ", "a")]
public void TestMethod1(string value1, string value2)
{
    Assert.AreEqual(value1 + value2, string.Concat(value1, value2));
}

解决方案

我知道这是一个老问题,但是现在在 https://blogs.msmvps.com/bsonnino/2017/03/18/parametrized-tests-with-ms-测试/

简而言之,您将需要安装MSTest.TestFrameworkMSTest.TestAdapter,并删除对Microsoft.VisualStudio.QualityTools.UnitTestFramework的引用.然后,您可以根据示例使用[DataTestMethod]属性指示参数化测试,并可以使用[DataRow]属性添加参数. [DataRow]属性的值将按照指定的顺序传递给测试方法.

请注意,[DataRow]属性中的值必须是基元,因此,例如,您不能使用DateTimedecimal.如果需要它们,则必须解决此限制(例如,可以用三个整数参数分别表示年,月和日,而不是使用DateTime参数来表示日期,并在测试中创建DateTime身体).

I have seen in a Microsoft video about Visual Studio update 2 regarding these attributes. However, I can't find any other information about them and can't get a project to build with them.

Does anyone know anything about these attributes or how to get them working?

[DataTestMethod]
[DataRow("a", "b")]
[DataRow(" ", "a")]
public void TestMethod1(string value1, string value2)
{
    Assert.AreEqual(value1 + value2, string.Concat(value1, value2));
}

解决方案

I know this is an old question, but there is now a good walkthrough published at https://blogs.msmvps.com/bsonnino/2017/03/18/parametrized-tests-with-ms-test/

In a nutshell, you will need to install MSTest.TestFramework and MSTest.TestAdapter, and remove references to Microsoft.VisualStudio.QualityTools.UnitTestFramework. You can then indicate a parameterised test with the [DataTestMethod] attribute, and can add your parameters using the [DataRow] attribute, as per your example. The values from the [DataRow] attribute will be passed to the test method in the order in which they are specified.

Note that the values in the [DataRow] attribute must be primitives, so you can't use a DateTime or decimal for example. If you want them, you will have to work around this limitation (e.g. instead of having a DateTime parameter to represent a date, you could have three integer parameters representing year, month and day, and create the DateTime within the test body).

这篇关于MSTEST中的DataTestMethod和DataRow属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆