简体   繁体   English

MSTest V2 按顺序执行单元测试 -> [DoNotParallelize]

[英]MSTest V2 Execute UnitTests sequentially -> [DoNotParallelize]

I have a question on running UnitTests sequentially.我对按顺序运行 UnitTests 有疑问。 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.该项目是在 .NET 核心 3.1 中编写的,单元测试需要在单元测试运行前后执行数据库操作。

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):在阅读https://www.meziantou.net/mstest-v2-execute-tests-in-parallel.htm和许多其他关于顺序单元测试的文章后,我想出了这个(简化):

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.当我执行测试 class 中的所有测试时,在运行 TestCleanup 之前,基 class 的 TestInitialize 被调用了两次。 This causes the CreateTestData method to fail as indexes prevent a double insert of the test data.这会导致 CreateTestData 方法失败,因为索引会阻止测试数据的双重插入。

What I would expect:我期望的是:

  • TestInitialize1 is called调用 TestInitialize1
  • TestMethod1 is executed执行 TestMethod1
  • TestCleanup1 is called调用 TestCleanup1
  • TestInitialize2 is called调用 TestInitialize2
  • TestMethod2 is executed执行 TestMethod2
  • TestCleanup2 is called调用 TestCleanup2
  • ... ...

What happens:发生什么了:

  • TestInitialize1 is called调用 TestInitialize1
  • TestMethod1 is executed执行 TestMethod1
  • TestInitialize2 is called before TestCleanup1 is called在调用 TestCleanup1 之前调用 TestInitialize2
  • TestMethod2 execution fails TestMethod2 执行失败

Am I missunderstanding the [DoNotParallelize] option?我是否误解了 [DoNotParallelize] 选项?

Paralelism isn't the problem here, my tests are definitely sequential and [ClassCleanup] also screwed me over.并行不是这里的问题,我的测试绝对是顺序的,[ClassCleanup] 也把我搞砸了。 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.我将尝试使用有序测试并更新答案,我现在能告诉你的最好的事情就是不要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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