C# 如何在 MSTest 中运行具有多个参数的测试方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9021881/
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
How to run a test method with multiple parameters in MSTest?
提问by The Light
NUnit has a feature called Values, like below:
NUnit 有一个名为 Values 的功能,如下所示:
[Test]
public void MyTest(
[Values(1,2,3)] int x,
[Values("A","B")] string s)
{
// ...
}
This means that the test method will run 6 times:
这意味着测试方法将运行 6 次:
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
We're using MSTest now, is there any equivalent for this so that I can run the same test with multiple parameters?
我们现在正在使用 MSTest,是否有任何等价物,以便我可以使用多个参数运行相同的测试?
[TestMethod]
public void Mytest()
{
// ...
}
采纳答案by jeroenh
It is unfortunately not supported in MSTest. Apparently there is an extensibility model and you can implement it yourself. Another option would be to use data-driven tests.
不幸的是,它在 MSTest 中不受支持。显然有一个可扩展性模型,您可以自己实现它。另一种选择是使用数据驱动的测试。
My personal opinion would be to just stick with NUnit though...
不过,我个人的意见是坚持使用 NUnit...
EDIT: As of Visual Studio 2012, update 1, MSTest has a similar feature. See @McAden's answerbelow.
编辑:从 Visual Studio 2012 更新 1 开始,MSTest 具有类似的功能。请参阅下面的@McAden 的回答。
回答by Massimiliano Peluso
MsTest does not support that feature but you can implement your own attribute to achieve that. have a look at the below:
MsTest 不支持该功能,但您可以实现自己的属性来实现该功能。看看下面的内容:
http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html
http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html
回答by k.m
Since nobody mentioned - not exactly the same as NUnit's Value(or TestCase) attributes, but MSTest has DataSourceattribute, which allows you to do similar thing. You can hook it up to database or XML file - not as straightforward as NUnit's feature, but does the job.
由于没有人提到 - 与 NUnit 的Value(或TestCase)属性不完全相同,但 MSTest 具有DataSource属性,它允许您做类似的事情。您可以将它连接到数据库或 XML 文件 - 不像 NUnit 的功能那么简单,但可以完成工作。
回答by Pritam Karmakar
MSTest has a powerful attribute called DataSource, using this you can perform data driven test as you asked. You can have your test data in XML, CSV or in a database. Here are few links that will guide you
MSTest 有一个强大的属性叫做DataSource,使用它你可以按照你的要求执行数据驱动测试。您可以将测试数据保存在 XML、CSV 或数据库中。这里有几个链接可以指导您
http://visualstudiomagazine.com/articles/2009/09/15/unit-testing-with-vsts2008-part-3.aspxhttp://msdn.microsoft.com/en-us/library/ms182527.aspx
http://msdn.microsoft.com/en-us/library/ms243192.aspx
http://visualstudiomagazine.com/articles/2009/09/15/unit-testing-with-vsts2008-part-3.aspx http://msdn.microsoft.com/en-us/library/ms182527.aspx
http: //msdn.microsoft.com/en-us/library/ms243192.aspx
Hope this will help you.
希望这会帮助你。
回答by McAden
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/
编辑 4:看起来这已在 2016 年 6 月 17 日的 MSTest V2 中完成: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:
大约一周前,在 Visual Studio 2012 Update 1 中,现在可以实现类似的功能:
[DataTestMethod]
[DataRow(12,3,4)]
[DataRow(12,2,6)]
[DataRow(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
EDIT: It appears this is only available within the unit testing project for WinRT/Metro. Bummer
编辑:看来这仅在WinRT/Metro的单元测试项目中可用。无赖
EDIT 2: The following is the metadata found using "Go To Definition" within Visual Studio:
编辑 2:以下是在 Visual Studio 中使用“转到定义”找到的元数据:
#region Assembly Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll, v11.0.0.0
// C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll
#endregion
using System;
namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodAttribute : TestMethodAttribute
{
public DataTestMethodAttribute();
public override TestResult[] Execute(ITestMethod testMethod);
}
}
EDIT 3: This issue was brought up in Visual Studio's UserVoice forums. Last Update states:
编辑 3:这个问题是在 Visual Studio 的 UserVoice 论坛中提出的。最后更新状态:
STARTED · Visual Studio Team ADMIN Visual Studio Team (Product Team, Microsoft Visual Studio) responded · April 25, 2016 Thank you for the feedback. We have started working on this.
Pratap Lakshman Visual Studio
已开始 · Visual Studio 团队 ADMIN Visual Studio 团队(产品团队,Microsoft Visual Studio)已回复 · 2016 年 4 月 25 日 感谢您的反馈。我们已经开始着手这方面的工作。
普拉塔普·拉克什曼 Visual Studio
回答by Soumya Dutta
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.
当然,还有另一种方法可以做到这一点,该方法尚未在本线程中讨论,即通过继承包含 TestMethod 的类的方式。在下面的示例中,只定义了一个 TestMethod,但已经创建了两个测试用例。
In Visual Studio 2012, it creates two tests in the TestExplorer:
在 Visual Studio 2012 中,它在 TestExplorer 中创建了两个测试:
- DemoTest_B10_A5.test
DemoTest_A12_B4.test
public class Demo { int a, b; public Demo(int _a, int _b) { this.a = _a; this.b = _b; } public int Sum() { return this.a + this.b; } } public abstract class DemoTestBase { Demo objUnderTest; int expectedSum; public DemoTestBase(int _a, int _b, int _expectedSum) { objUnderTest = new Demo(_a, _b); this.expectedSum = _expectedSum; } [TestMethod] public void test() { Assert.AreEqual(this.expectedSum, this.objUnderTest.Sum()); } } [TestClass] public class DemoTest_A12_B4 : DemoTestBase { public DemoTest_A12_B4() : base(12, 4, 16) { } } public abstract class DemoTest_B10_Base : DemoTestBase { public DemoTest_B10_Base(int _a) : base(_a, 10, _a + 10) { } } [TestClass] public class DemoTest_B10_A5 : DemoTest_B10_Base { public DemoTest_B10_A5() : base(5) { } }
- DemoTest_B10_A5.test
DemoTest_A12_B4.test
public class Demo { int a, b; public Demo(int _a, int _b) { this.a = _a; this.b = _b; } public int Sum() { return this.a + this.b; } } public abstract class DemoTestBase { Demo objUnderTest; int expectedSum; public DemoTestBase(int _a, int _b, int _expectedSum) { objUnderTest = new Demo(_a, _b); this.expectedSum = _expectedSum; } [TestMethod] public void test() { Assert.AreEqual(this.expectedSum, this.objUnderTest.Sum()); } } [TestClass] public class DemoTest_A12_B4 : DemoTestBase { public DemoTest_A12_B4() : base(12, 4, 16) { } } public abstract class DemoTest_B10_Base : DemoTestBase { public DemoTest_B10_Base(int _a) : base(_a, 10, _a + 10) { } } [TestClass] public class DemoTest_B10_A5 : DemoTest_B10_Base { public DemoTest_B10_A5() : base(5) { } }
回答by Andrey Burykin
It's very simple to implement - you should use TestContextproperty and TestPropertyAttribute.
实现起来非常简单 - 您应该使用TestContextproperty 和TestPropertyAttribute.
Example
例子
public TestContext TestContext { get; set; }
private List<string> GetProperties()
{
return TestContext.Properties
.Cast<KeyValuePair<string, object>>()
.Where(_ => _.Key.StartsWith("par"))
.Select(_ => _.Value as string)
.ToList();
}
//usage
[TestMethod]
[TestProperty("par1", "http://getbootstrap.com/components/")]
[TestProperty("par2", "http://www.wsj.com/europe")]
public void SomeTest()
{
var pars = GetProperties();
//...
}
回答by Pompair
This feature is in pre-releasenow and works with VS 2015.
此功能现在处于预发布状态,适用于 VS 2015。
For example:
例如:
[TestClass]
public class UnitTest1
{
[DataTestMethod]
[DataRow(1, 2, 2)]
[DataRow(2, 3, 5)]
[DataRow(3, 5, 8)]
public void AdditionTest(int a, int b, int result)
{
Assert.AreEqual(result, a + b);
}
}
回答by Brandon
I couldn't get The DataRowAttributeto work in Visual Studio 2015, this is what I ended up with:
我无法让 TheDataRowAttribute在 Visual Studio 2015 中工作,这就是我最终得到的结果:
[TestClass]
public class Tests
{
private Foo _toTest;
[TestInitialize]
public void Setup()
{
this._toTest = new Foo();
}
[TestMethod]
public void ATest()
{
this.Perform_ATest(1, 1, 2);
this.Setup();
this.Perform_ATest(100, 200, 300);
this.Setup();
this.Perform_ATest(817001, 212, 817213);
this.Setup();
}
private void Perform_ATest(int a, int b, int expected)
{
//Obviously this would be way more complex...
Assert.IsTrue(this._toTest.Add(a,b) == expected);
}
}
public class Foo
{
public int Add(int a, int b)
{
return a + b;
}
}
The real solution here is to just use NUnit (unless you're stuck in MSTest like I am in this particular instance).
这里真正的解决方案是只使用 NUnit(除非你像我在这个特定实例中一样被困在 MSTest 中)。

