theateist
theateist

Reputation: 14399

How ClassInitialize/TestInitialize insures the test won't run in parallel?

My integration tests must be run sequentially (the order doesn't matter) because each test accesses external hardware. When the hardware is in use, no other test should access it until previous test finished.

I've thought about two solutions below, but I really don't like them. Both are not scalable. Also, for the first solution I'll have to wrap each test in try-catch so I'll know which one failed.

Is there a better solution? Some people suggested using ClassInitialize or TestInitialize, but I don't understand how it insures the test won't run in parallel? The classes decorated with ClassInitialize will run in parallel anyway, won't they?

I currently use MSTest, but can switch to xUnit or NUnit if needed.

Solution 1

public void Main()
{ 
   try
   { 
      IntegrationTest1();
      IntegrationTest2();
      IntegrationTest2();
   }
   catch(){}
}

Solution2

[TestMethod]
public void IntegrationTests()
{
    IntegrationTest1();
    IntegrationTest2();
    IntegrationTestN();
}

The tests will started by a third party like `mstest myintegrationtest.dll should be able run

Upvotes: 1

Views: 463

Answers (2)

Micke
Micke

Reputation: 27

In MSTest annotate class or method with [DoNotParallelize]

[TestClass]
[DoNotParallelize]
public class MyTestClass
{

[TestMethod]
[DoNotParallelize]
public void MyTest()
{}

}

Upvotes: 0

Milad Karimifard
Milad Karimifard

Reputation: 440

By default, tests are run one by one in the test class and the run order is alphabetically according to the Microsoft document.

With MSTest, tests are automatically ordered by their test name.

Document link https://learn.microsoft.com/en-us/dotnet/core/testing/order-unit-tests?pivots=mstest

About running tests sequential or parallel, in my experience & what I find out when researching about this, the tests won't run in parallel unless you specify it. this is document link about active parallel execution in MsTest

Document link https://devblogs.microsoft.com/devops/mstest-v2-in-assembly-parallel-test-execution/

ClassInitialize/Testinitialize & ClassCleanup/TestCleanup attributes are used for scenarios you want to do something before the Test Class (Collection of test)/Test method (Single Test) load or after execution of Test(s) complete. the goal of all of these is to isolate your test at method/class level & it's not relevant to the order of execution.

If order of exception is important for you I suggest switching your test framework to xUint because in xUint the order of running tests is the order of defining tests in class (order of definition in test class)

Upvotes: 1

Related Questions