MsTest并行化

时间:2019-06-23 17:03:28

标签: c# .net parallel-processing mstest

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

我希望Test1会失败,但是事实证明MsTestV2创建了测试类的新实例,因此成员变量没有共享。

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

[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框架保证的测试类的新实例?

1 个答案:

答案 0 :(得分:0)

MSTest为每个测试方法创建[TestClass]的新实例。该行为在MSTestV2中也将继续。 .runsettings不会影响此行为。
请考虑在MSTestV2 doc repo上提出问题,以澄清文档。