MsTest并行化

MsTest parallelization

我运行两个并行共享资源的测试。

我预计 Test1 会失败,但事实证明 MsTestV2 创建了一个新的测试实例 class 因此成员变量未共享。

这是通过检查每个测试的对象哈希码来验证的,并且还检查构造函数被调用了多少次。

[TestClass]
public class ParallelizationTest
{
    private int psw = 0;

    public ParallelizationTest()
    {
        Console.Out.WriteLine("Ctor: ParallelizationTest");
    }

    [TestMethod]
    public void Test1()
    {
        Console.Out.WriteLine("Test1 started with HashCode: " + GetHashCode());
        Thread.Sleep(5000);
        Assert.AreEqual(psw, 0);
        Console.Out.WriteLine("Test1 ended");
    }

    [TestMethod]
    public void Test2()
    {
        Console.Out.WriteLine("Test2 started with HashCode: " + GetHashCode());
        psw = 123;
        Assert.AreEqual(psw, 123);
        Console.Out.WriteLine("Test2 ended");
    }
}

MsTestV2配置为(Workers设置为2):

  <RunSettings>
  <RunConfiguration>
    <TargetPlatform>x86</TargetPlatform>
    <MaxCpuCount>1</MaxCpuCount>
    <DisableParallelization>false</DisableParallelization>
    <TestSessionTimeout>1000000</TestSessionTimeout>
  </RunConfiguration>
  <MSTest>
    <Parallelize>
      <Workers>2</Workers>
      <Scope>MethodLevel</Scope>
    </Parallelize>
  </MSTest>
</RunSettings>

MsTest 框架是否保证创建新的测试实例class?

MSTest 为每个测试方法创建一个新的 [TestClass] 实例。这种行为也适用于 MSTestV2。 .runsettings 不会影响此行为。
请考虑在 MSTestV2 doc repo 上提交问题以澄清文档。