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

标签 c# nunit

我在 Visual Studio Professional 2017 中使用 NUnit 3.8

我在我的 AssemblyInfo.cs 文件中指定了 [assembly: Parallelizable(ParallelScope.Fixtures)]。这使得 类中测试的默认行为无法与该类中的其他测试并行运行,但它们可以与其他类的测试并行运行。

我想知道是否可以保留默认行为,但在逐个类的基础上反转逻辑。我希望在特定类中的测试与另一个并行运行,但不与其他类的测试并行运行。

接受下面的代码。如果我要运行下面的所有测试——我希望 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);
    }        
}

基于 this NUnit documentation (具体摘录如下),我认为这是可能的。

Non-parallel class with parallel methods:
The methods only run in parallel with one another, not with the test methods of any other classes.

但是,当我用三个工作线程运行上面的代码时,前三个测试成功运行,然后不再启动测试。在我取消测试运行之前,测试运行处于不确定状态。

有什么建议吗?

最佳答案

您的代码示例看起来正确。有一些错误,例如 https://github.com/nunit/nunit/issues/2438这导致了 v3.8 中测试用例并行化的问题。该团队目前正在开发 v3.8.2,应该可以解决这些问题。

许多问题已经得到解决 - 您可能希望尝试最新版本的 master 以查看您的情况是否已得到解决:

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

关于c# - 在类中并行运行 NUnit 测试但不与其他类测试一起运行的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46554172/

相关文章:

c# - Xamarin Forms 中的手势图像

c# - NUnit visual studio 扩展的混合模式错误

c# - Moq 简单示例所需 - 数据库连接和文件处理

C# 文档 : <see> or <seealso> for a operation

c# - btnClose 设置为 CausesValidation = false 但不起作用

c# - 如何从方法返回文件类型

c# - IsNullOrEmpty 对象

.net - MVC 4 模拟 HttpContext - 如何模拟 DisplayModeProvider

c# - NSubstitute 与 PRISM EventAggregator : Assert that calling a method triggers event with correct payload

unit-testing - 既然我已经将所有单元测试迁移到 MSpec,我还需要 NUnit 吗?