Search code examples
c#.netmstest

In MSTest, how can I specify that certain test methods cannot be run in parallel with each other?


I have a large set of integration tests that test a website server. Most of these tests are fine to run in parallel. However, I have a few that change settings and can cause each other to fail when run in parallel.

As a simplified example, let's say I had these tests:

TestPrice_5PercentTax
TestPrice_10PercentTax
TestPrice_NoTax
TestInventory_Add10Items
TestInventory_Remove10Items

The inventory tests will not get in the way of each other, and are not affected by the price tests. But the price tests will change the Tax setting, so that if both 5 and 10 run in parallel, 10 could end up changing the setting before 5 is done, and 5 would fail because it saw 10% tax instead of the 5% it expected.

I want to define a category for the three price tests, and say that they may not run at the same time as one another. They can run at the same time as any other tests, just not the other price tests. Is there a way to do this in MSTest?


Solution

  • MsTest v2 has functionality as following

    [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
    // Notice the assembly bracket, this can be compatible or incompatible with how your code is built
    
    namespace UnitTestProject1
    {
        [TestClass]
        public class TestClass1
        {
            [TestMethod]
            [DoNotParallelize] // This test will not be run in parallel
            public void TestPrice_5PercentTax() => //YourTestHere?;
    
            [TestMethod]
            [DoNotParallelize] // This test will not be run in parallel
            public void TestPrice_10PercentTax() => //YourTestHere?;            
    
            [TestMethod]
            [DoNotParallelize] // This test will not be run in parallel
            public void TestPrice_NoTax() => //YourTestHere?;
    
            [TestMethod]
            public void TestInventory_Add10Items() => //YourTestHere?;
    
            [TestMethod]
            public void TestInventory_Remove10Items() => //YourTestHere?;
        }
    }
    

    More detailed information can be found here MSTest v2 at meziantou.net

    I strongly recommend atleast a quick read through of the link, as this will likely help you solve and understand the issue with the tests run in parallel or sequential.