C# MSTEST 中的 DataTestMethod 和 DataRow 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15921226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:27:47  来源:igfitidea点击:

DataTestMethod and DataRow attributes in MSTEST

c#unit-testingmstest

提问by Simon Vane

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.

我在有关这些属性的 Visual Studio 更新 2 的 Microsoft 视频中看到过。但是,我找不到有关它们的任何其他信息,也无法使用它们构建项目。

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));
}

采纳答案by Tim

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/

我知道这是一个老问题,但现在有一个很好的演练发布在https://blogs.msmvps.com/bsonnino/2017/03/18/parametrized-tests-with-ms-test/

In a nutshell, you will need to install MSTest.TestFrameworkand 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.

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

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

请注意,[DataRow]属性中的值必须是基元,因此不能使用 aDateTimedecimal例如。如果你想要它们,你将不得不解决这个限制(例如DateTime,你可以用三个整数参数来表示年、月和日,而不是用一个参数来表示日期,并DateTime在测试主体内创建)。

回答by Simon Vane

It appears this is only available within the unit testing project for WinRT/Metro and now with update 2, Windows Phone 8. It's a mystery to my why this is not available for all testing with mstest.

看来这仅在 WinRT/Metro 的单元测试项目中可用,现在在更新 2、Windows Phone 8 中可用。为什么这不适用于 mstest 的所有测试,这对我来说是个谜。

回答by ironstone13

Finally, this feature has been added (still in pre-release) https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/

最后,此功能已添加(仍在预发布中) https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/

Basically, one has to do two things:

基本上,一个人必须做两件事:

1) Install two NuGet packages (versions don't really matter, but this is what I have)

1)安装两个 NuGet 包(版本并不重要,但这是我所拥有的)

  <package id="MSTest.TestAdapter" version="1.1.5-preview" targetFramework="net452" />
  <package id="MSTest.TestFramework" version="1.0.6-preview" targetFramework="net452" />

2) Remove the refenrece to the old test library, because it has the same attributes defined in the same namespaces - this was done to achieve backwards compatibility

2) 删除对旧测试库的引用,因为它在相同的命名空间中定义了相同的属性 - 这样做是为了实现向后兼容性

Microsoft.VisualStudio.QualityTools.UnitTestFramework