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)); }
A test method can execute the same code but have different input arguments. You can use the DataRow attribute to specify values for those inputs. Instead of creating new tests, apply these two attributes to create a single data driven test.
The method decorated by [ClassInitialize] is called once before running the tests of the class. In some cases, you can write the code in the constructor of the class. The method decorated by [ClassCleanup] is called after all tests from all classes are executed.
The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.
TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With