How to run a test method with several parameters in MSTest?

NUnit has a function called "Values", as shown below:

[Test] public void MyTest( [Values(1,2,3)] int x, [Values("A","B")] string s) { // ... } 

This means that the verification method will be executed 6 times:

 MyTest(1, "A") MyTest(1, "B") MyTest(2, "A") MyTest(2, "B") MyTest(3, "A") MyTest(3, "B") 

Now we are using MSTest, is there an equivalent for this so that I can run the same test with several parameters?

 [TestMethod] public void Mytest() { // ... } 
+85
c # unit-testing nunit mstest vs-unit-testing-framework
Jan 26 '12 at 16:50
source share
10 answers

Unfortunately, it is not supported in MSTest. There seems to be 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 to NUnit, though ...

EDIT: with Visual Studio 2012 update 1, MSTest has a similar feature. See @McAden answer below.

+36
Jan 26 '12 at 16:55
source share

EDIT 4 : It looks like this completed in MSTest V2 on June 17, 2016: https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest- v2 /

Original answer :

After about a week, something similar is now possible in 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 seems that this is only available as part of the module testing project for WinRT / Metro . Lazy person

EDIT 2 . The following is the metadata found using Go To Definition in Visual Studio:

 #region Assembly Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll, v11.0.0.0 // C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.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 has occurred on the Visual Studio UserVoice forums. Last update:

GETTING STARTED Β· Visual Studio Team ADMIN Visual Studio Team (Team product, Microsoft Visual Studio) replied Β· April 25, 2016. Thanks for the feedback. We started working on it.

Pratap Lakshman Visual Studio

https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/3865310-allow-use-of-datatestmethod-datarow-in-all-unit

+83
Dec 04
source share

This feature is now in preview and works with 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); } } 
+10
Jun 17 '16 at 14:27
source share

Since no one is mentioned, it's not exactly the same as the NUnit Value (or TestCase ) attributes, but MSTest has a DataSource attribute, which allows you to do this. You can connect it to a database or XML file - not as simple as the NUnit function, but it does the job.

+9
Jan 26 '12 at 16:58
source share

MSTest has a powerful DataSource attribute with which you can perform data validation as you requested. You can have your test data in XML, CSV or in a database. Here are some links to help you.

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 helps you.

+6
Jan 27 '12 at 5:15
source share

MsTest does not support this function, but you can implement your own attribute to achieve this. look below:

http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html

+4
Jan 26 '12 at 16:55
source share

There is, of course, another way to do this, which was not discussed in this thread, that is, by inheriting a class containing TestMethod. In the following example, only one TestMethod was defined, but two test cases were made.

In Visual Studio 2012, it creates two tests in 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) { } } 
+4
Mar 25 '14 at 17:13
source share

It is very simple to implement - you must use the TestContext property and 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(); //... } 
+4
May 16 '16 at 7:48
source share

I couldn't get The DataRowAttribute to work in Visual Studio 2015, here's what I ended up with:

 [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 are stuck in MSTest, as I am in this particular case).

+3
Oct 27 '16 at 16:36
source share

You can implement a data-driven test, for example HERE . I do not know if there are such convenient ones as in NUnit ..

+1
Jan 26 '12 at 16:57
source share



All Articles