并行运行 xUnit 测试用例
[!IMPORTANT] xUnit.net v3 4.0.0 ships this functionality out of the box with the new
ParallelMode.Allparallel 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.ParallelTestFrameworkFor xUnit v2:
dotnet add package Meziantou.Xunit.ParallelTestFrameworkBy default, all tests will run in parallel.
Selectively disable parallelization by adding the DisableParallelization attribute to a collection or theory.
…`
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.
…`
The code is greatly inspired by the sample from Travis Mortimer:
[!IMPORTANT] This package requires the
preEnumerateTheoriesxUnit setting to be enabled (true). xUnit v3 will default this setting tofalsewhen 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 thepreEnumerateTheoriessetting in yourxunit.runner.jsonfile:{ "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", "preEnumerateTheories": true }
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> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)
{
var rows = new List
{
new TheoryDataRow(1),
new TheoryDataRow(2),
new TheoryDataRow(3),
};
return new ValueTask>(rows);
}
// Must return true for parallel execution of theory data rows
public override bool SupportsDiscoveryEnumeration() => true;
}Using the EnableParallelizationAttribute on an ICollectionFixture 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.
…暂无开放 Issues,或尚未同步最近议题。