63

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));
}
Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
Simon Vane
  • 1,914
  • 3
  • 19
  • 23
  • 1
    Check this: [How to run a test method with multiple parameters in MSTest?](http://stackoverflow.com/questions/9021881/how-to-run-a-test-method-with-multiple-parameters-in-mstest#13710788). – chaliasos Apr 10 '13 at 09:09
  • Thanks a lot. Any clue why its use is so limited? – Simon Vane Apr 10 '13 at 09:37

3 Answers3

74

There is a good walkthrough originally published at https://blogs.msmvps.com/bsonnino/2017/03/18/parametrized-tests-with-ms-test (link is now to archive by wayback machine).

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).

Tim
  • 5,435
  • 7
  • 42
  • 62
  • 6
    Thanks a lot for the answer Tim. If anyone is absolutely tied to MSTest for some reason then that's great news. If not then I'd strongly recommend changing to NUnit, XUnit etc. Microsoft simply do not take testing or TDD seriously. We changed a very large project over from MSTest to NUnit with very little work which opened up the doors to a a far more fully featured test framework that is being actively developed. It's taken 4 years for Microsoft to deliver this simple feature which speaks volumes. – Simon Vane Sep 18 '17 at 16:33
  • 1
    Passing in single `int` for `DateTime` tests helpful. In my case testing various hours 2, 3, 4. – SushiGuy Feb 16 '18 at 18:12
  • I use mostly a Dictionary for the values, so I can use a descriptive text in the DataRow for the values - e. g. DateTime - or a string that is finally parsed as a time, but here I failed once due to time zones - best use DateTimeOffset to have full control over the offset. – BitLauncher Dec 06 '19 at 13:18
18

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/

Basically, one has to do two things:

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

  <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

Microsoft.VisualStudio.QualityTools.UnitTestFramework
ironstone13
  • 3,325
  • 18
  • 24
  • 2
    Nicely written. With execution time for free; easy output logging and now features like `DataTestMethod` MSTest-v2 is a competitive unit test framework. – Aaron Jan 04 '17 at 03:47
9

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.

Simon Vane
  • 1,914
  • 3
  • 19
  • 23
  • 1
    Well it is now - or at least will be in the next version of MSTest https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/ – Dror Helper Aug 08 '16 at 08:26
  • 1
    It is now available as a NuGet package https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/ – ironstone13 Nov 23 '16 at 14:18