简体   繁体   English

在类中并行运行 NUnit 测试但不与其他类测试一起运行的方法?

[英]Way to run NUnit tests within class in parallel, but not run with other class tests?

I'm using NUnit 3.8 in Visual Studio Professional 2017 .我在Visual Studio Professional 2017使用NUnit 3.8

I've got [assembly: Parallelizable(ParallelScope.Fixtures)] designated in my AssemblyInfo.cs file.我在我的AssemblyInfo.cs文件中指定了[assembly: Parallelizable(ParallelScope.Fixtures)] This makes the default behavior that tests within classes cannot run in parallel with other tests within that class, but they can run in parallel with tests of other classes.这使得默认行为类的测试可以在这个类中的其他测试并行不运行,但是它们可以在平行与其他类的测试运行。

I'm wondering if it's possible to keep that default behavior but invert the logic on a class-by-class basis.我想知道是否有可能保持默认行为,但在逐个类的基础上反转逻辑。 I want to have tests within a specific class run in parallel with one another , but not with tests of other classes.我想让特定类中的测试彼此并行运行,但不与其他类的测试并行运行。

Take the code below.拿下面的代码。 If I were to run all the tests below - I would want all of the tests in TestClassA to execute in parallel until all were done, and THEN all tests from TestClassB would execute in parallel.如果我要运行下面的所有测试 - 我希望 TestClassA 中的所有测试并行执行,直到所有测试都完成,然后 TestClassB 中的所有测试将并行执行。

[NonParallelizable]
public class TestClassA
{
    [Test]
    [Parallelizable(ParallelScope.Children)]
    [TestCase(1, 1)]
    [TestCase(2, 2)]
    [TestCase(3, 3)]
    [TestCase(4, 4)]
    public void TestIntsA(int a, int b)
    {
        System.Threading.Thread.Sleep(1000);
        Assert.AreEqual(a, b);
    }        
}

[NonParallelizable]
public class TestClassB
{
    [Test]
    [Parallelizable(ParallelScope.Children)]
    [TestCase(1, 2)]
    [TestCase(2, 3)]
    [TestCase(3, 4)]
    [TestCase(4, 5)]
    public void TestIntsB(int a, int b)
    {
        System.Threading.Thread.Sleep(1000);
        Assert.AreEqual(a, b);
    }        
}

Based on this NUnit documentation (specific excerpt below), I would think this is possible.基于这个 NUnit 文档(具体摘录如下),我认为这是可能的。

Non-parallel class with parallel methods:具有并行方法的非并行类:
The methods only run in parallel with one another, not with the test methods of any other classes.这些方法仅彼此并行运行,而不与任何其他类的测试方法并行运行。

However, when I've run the above code with three worker threads, the first three tests run successfully, and then no more tests are initiated.但是,当我使用三个工作线程运行上述代码时,前三个测试成功运行,然后不再启动更多测试。 The test run hangs in limbo until I cancel the test run.测试运行一直处于不确定状态,直到我取消测试运行。

Any suggestions?有什么建议?

You code sample looks correct.您的代码示例看起来正确。 There's been a few bugs such as https://github.com/nunit/nunit/issues/2438 which have caused problems with test case parallelization in v3.8.有一些错误,例如https://github.com/nunit/nunit/issues/2438 ,导致 v3.8 中的测试用例并行化出现问题。 The team are working on v3.8.2 at the moment, which should resolve these issues.该团队目前正在开发 v3.8.2,应该可以解决这些问题。

A number of problems have been resolved already - you may wish to try the latest build of master to see if your case has been fixed:已经解决了许多问题 - 您可能希望尝试使用最新版本的 master 来查看您的情况是否已修复:

https://ci.appveyor.com/project/CharliePoole/nunit/build/3.9.0-dev-04489/artifacts https://ci.appveyor.com/project/CharliePoole/nunit/build/3.9.0-dev-04489/artifacts

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

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