C
C#4w ago
Core

✅ Xunit v3 running test cases in parallel

Hello, tests are running in parallel, but not test cases. One of my tests has to process more than 50 fixture files, and it currently runs synchronously. How can I make it run in parallel?
c#
[Theory]
[MemberData(nameof(FixtureFileNames))]
public async Task TryParse_ShouldProduceConsistentResultsAcrossParsers(string fileName)
{
var fixturePath = Path.Combine("Fixtures", "Collections", $"{fileName}.json");
var fixtures = await FixtureLoader.LoadAsync<UserAgentFixture>(fixturePath);

// assertion
}

public static TheoryData<string> FixtureFileNames =>
[
"cameras",
"car_browsers",
"client_hints",
"client_hints_apps",
"consoles",
"desktops",
"desktops_1",
"feature_phones",
"feed_readers",
"media_players",
]
c#
[Theory]
[MemberData(nameof(FixtureFileNames))]
public async Task TryParse_ShouldProduceConsistentResultsAcrossParsers(string fileName)
{
var fixturePath = Path.Combine("Fixtures", "Collections", $"{fileName}.json");
var fixtures = await FixtureLoader.LoadAsync<UserAgentFixture>(fixturePath);

// assertion
}

public static TheoryData<string> FixtureFileNames =>
[
"cameras",
"car_browsers",
"client_hints",
"client_hints_apps",
"consoles",
"desktops",
"desktops_1",
"feature_phones",
"feed_readers",
"media_players",
]
xunit.runner.json is also loaded
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"parallelizeAssembly": true,
"parallelizeTestCollections": true,
"maxParallelThreads": "unlimited"
}
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"parallelizeAssembly": true,
"parallelizeTestCollections": true,
"maxParallelThreads": "unlimited"
}
2 Replies
hamarb123
hamarb1234w ago
you cannot run multiple tests within a class simultaneously in xunit iirc - if you want them to run in parallel, you can do something along these lines iirc:
public abstract class MyBase(string fileName)
{
[Fact]
public async Task TryParse_ShouldProduceConsistentResultsAcrossParsers()
{
...
}
}
public abstract class MyBase(string fileName)
{
[Fact]
public async Task TryParse_ShouldProduceConsistentResultsAcrossParsers()
{
...
}
}
and then instantiate them all with 1 line each like so:
public class MyBase_cameras : MyBase("cameras");
public class MyBase_car_browsers : MyBase("car_browsers");
...
public class MyBase_cameras : MyBase("cameras");
public class MyBase_car_browsers : MyBase("car_browsers");
...
here's an example where I did this to get stuff to run in parallel: https://github.com/hamarb123/hamarb123.ByRefHandles/blob/main/hamarb123.ByRefHandles.Test/ByRefHandleTest.cs
Core
CoreOP4w ago
Thanks for the help

Did you find this page helpful?