简体   繁体   中英

MSTest V2 Execute UnitTests sequentially -> [DoNotParallelize]

I have a question on running UnitTests sequentially. Unfortunately in scenario it is not an option to run them parallel or mock the database. The project is written in .NET core 3.1 and the UnitTests need to execute database operations before and after a Unittest has run.

After reading https://www.meziantou.net/mstest-v2-execute-tests-in-parallel.htm and a lot of other articles about sequential UnitTesting I came up with this (simplified):

BaseClass:

namespace XY.Test
{
    [TestClass]
    public class BaseTest: TimerModel
    {
        private static readonly DbCreator Creator = new DbCreator();
        public static readonly DbConnectionManager ConnectionManager = new DbConnectionManager();

        [TestInitialize]
        public void BaseTestInitialize()
        {
            CreateTestData();
        }

        [TestCleanup]
        public void BaseTestCleanup()
        {
            RemoveTestData();
        }

        public void CreateTestData()
        {
            RemoveTestData();
            Creator.ExecuteSqlFromScript(ConnectionManager, @"Resources\CreateTestData.sql");
        }

        public void RemoveTestData()
        {
            Creator.ExecuteSqlFromScript(ConnectionManager, @"Resources\EmptyTestDataTables.sql");
        }
    }
}

TestClass:

[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)] //<-- Also tried out Workers = 1 and Scope = ExecutionScope.MethodLevel
namespace XY.Test.Models
{
    [TestClass]
    public class TerminalConfigModelTest: BaseTest
    {
        [TestMethod]
        [DoNotParallelize]
        public void TestMethod1()
        {
            ...
        }

        [TestMethod]
        [DoNotParallelize]
        public void TestMethod2()
        {
            ...
        }
    }
}

For some reason, no matter what I do, the UnitTests are being executed parallel. What do I have to change in order to have them executed sequentially?

When I execute all tests in the test class, the TestInitialize of the base class is called twice before the TestCleanup is run. This causes the CreateTestData method to fail as indexes prevent a double insert of the test data.

What I would expect:

  • TestInitialize1 is called
  • TestMethod1 is executed
  • TestCleanup1 is called
  • TestInitialize2 is called
  • TestMethod2 is executed
  • TestCleanup2 is called
  • ...

What happens:

  • TestInitialize1 is called
  • TestMethod1 is executed
  • TestInitialize2 is called before TestCleanup1 is called
  • TestMethod2 execution fails

Am I missunderstanding the [DoNotParallelize] option?

Paralelism isn't the problem here, my tests are definitely sequential and [ClassCleanup] also screwed me over. It's just unintuitive and weird, more info here . I will try using ordered tests and update the answer, best thing i can tell you now is just don't use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM