Skip to content

Latest commit

 

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Important

xUnit.net v3 4.0.0 ships this functionality out of the box with the new ParallelMode.All parallel mode. If you target xUnit v3 4.0.0 or later, you no longer need this package — see MIGRATION.md for how to move to the built-in feature.

By default, xUnit runs all test cases in a test class synchronously. This package extends the default test framework to execute tests in parallel.

For xUnit v3:

dotnet add package Meziantou.Xunit.v3.ParallelTestFramework

For xUnit v2:

dotnet add package Meziantou.Xunit.ParallelTestFramework

By default, all tests will run in parallel. Selectively disable parallelization by adding the DisableParallelization attribute to a collection or theory.

// All tests are run in parallel
public class ParallelTests
{
    [Fact]
    public void Test1() => Thread.Sleep(2000);

    [Fact]
    public void Test2() => Thread.Sleep(2000);

    [Theory]
    [InlineData(0), InlineData(1), InlineData(2)]
    public void Test3(int value) => Thread.Sleep(2000);

    // This test runs in parallel with other tests
    // However, its test cases are run sequentially
    [Theory]
    [DisableParallelization]
    [InlineData(0), InlineData(1), InlineData(2)]
    public void Test4(int value) => Thread.Sleep(2000);
}

// This collection runs in parallel with other collections
// However, its tests cases are run sequentially
[DisableParallelization]
public class SequentialTests
{
    [Fact]
    public void Test1() => Thread.Sleep(2000);

    [Fact]
    public void Test2() => Thread.Sleep(2000);
}

Previous versions of this package relied on built in xUnit attributes instead of exposing a dedicated DisableParallelization attribute. For backwards compatibility, parallelization can also be disabled by adding an explicit Collection attribute or Theory attribute with DisableDiscoveryEnumeration enabled.

// All tests are run in parallel
public class ParallelTests
{
    [Fact]
    public void Test1() => Thread.Sleep(2000);

    [Fact]
    public void Test2() => Thread.Sleep(2000);

    [Theory]
    [InlineData(0), InlineData(1), InlineData(2)]
    public void Test3(int value) => Thread.Sleep(2000);

    // This test runs in parallel with other tests
    // However, its test cases are run sequentially because of DisableDiscoveryEnumeration
    [Theory]
    [MemberData(nameof(GetData), DisableDiscoveryEnumeration = true)]
    public void Test4(int value) => Thread.Sleep(2000);

    public static TheoryData<int> GetData() =>  new() { { 0 }, { 1 } };
}

// This collection runs in parallel with other collections
// However, its tests cases are run sequentially because the Collection is explicit
[Collection("Sequential")]
public class SequentialTests
{
    [Fact]
    public void Test1() => Thread.Sleep(2000);

    [Fact]
    public void Test2() => Thread.Sleep(2000);
}

The code is greatly inspired by the sample from Travis Mortimer: xunit/xunit#1986 (comment)

Important

This package requires the preEnumerateTheories xUnit setting to be enabled (true). xUnit v3 will default this setting to false when using it with Microsoft Testing Platform (instead of VSTest). If this setting is not enabled, you may get unexpected results. To ensure the correct behavior, you must enable the preEnumerateTheories setting in your xunit.runner.json file:

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "preEnumerateTheories": true
}

Custom DataAttribute

When creating a custom DataAttribute, you must ensure that the SupportsDiscoveryEnumeration() method returns true for theory data rows to run in parallel. If this method returns false, the theory data rows will run sequentially.

public sealed class CustomDataAttribute : DataAttribute
{
    public override ValueTask<IReadOnlyCollection<ITheoryDataRow>> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)
    {
        var rows = new List<ITheoryDataRow>
        {
            new TheoryDataRow(1),
            new TheoryDataRow(2),
            new TheoryDataRow(3),
        };
        return new ValueTask<IReadOnlyCollection<ITheoryDataRow>>(rows);
    }

    // Must return true for parallel execution of theory data rows
    public override bool SupportsDiscoveryEnumeration() => true;
}

Parallel in a collection

Using the EnableParallelizationAttribute on an ICollectionFixture<T> enables the parallel execution between classes in a collection. If you want to enable parallisation inside a class you still need to add the EnableParallelizationAttribute on the test class aswell.

[CollectionDefinition("MyFixture Collection")]
[EnableParallelization] // This enables the parallel execution of classes in a collection 
public class MyFixtureCollection : ICollectionFixture<MyFixture>
{
}

[Collection("MyFixture Collection")]
public class MyFirstTestClass
{
    private readonly MyFixture fixture;

    public ParallelCollectionMultiClass1AttributeTests(MyFixture fixture)
    {
        this.fixture = fixture;
    }

    //...
}

[Collection("MyFixture Collection")]
public class MySecondTestClass
{
    private readonly MyFixture fixture;

    public ParallelCollectionMultiClass1AttributeTests(MyFixture fixture)
    {
        this.fixture = fixture;
    }

    //...
}

About

Run xUnit test cases in parallel

Resources

Contributing

Stars

185 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages