Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTestMethod vs TestMethod

Tags:

I started to use MSTest 2 DataRow attributes to check multiple cases in a single test:

[TestMethod] [DataRow(1, 1, 2)] [DataRow(1, 2, 3)] public void AdditionWorks(int op1, int op2, int expectedResult) {     Assert.AreEqual(expectedResult, new Sut().Add(op1, op2)); } 

It works nicely, both in NCrunch and in CI. Only now I noticed that there is special attribute DataTestMethod that is supposed to mark such tests instead of TestMethod.

Is there a difference? A reason to use one variant in particular?

like image 417
Lukáš Lánský Avatar asked Oct 24 '17 07:10

Lukáš Lánský


People also ask

Does TestInitialize run for each test?

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.

What is TestContext C#?

Each NUnit test runs in an execution context, which includes information about the environment as well as the test itself. The TestContext class allows tests to access certain information about the execution context.


2 Answers

Both attributes work because the same attributes are defined in the same namespace as the previous version of MSTest. This was done for backward compatibility.

Reference :

Taking the MSTest Framework forward with “MSTest V2”

Github: Unit Test Samples

like image 142
Nkosi Avatar answered Sep 22 '22 06:09

Nkosi


ShreyasRmsft commented the following on GitHub:

Hi @cactuaroid DataTestMethod is not needed. Go ahead and use TestMethod with DataRows to data drive your tests. For any more doubts follow the official docs at https://github.com/microsoft/testfx-docs

https://github.com/microsoft/testfx/issues/614

https://github.com/microsoft/testfx-docs/issues/64

So, according to Microsoft, it is preferred to use TestMethod over DataTestMethod.

like image 30
Nathan Avatar answered Sep 20 '22 06:09

Nathan