Mats
Mats

Reputation: 14817

SpecFlow with xUnit - How to create test collections to prevent parallel execution of tests?

I have a test project (.Net 7) with a bunch of SpecFlow features. Upon build the *.cs files for those features are generated. As far as I know there is only one way to tell xUnit to run tests in sequence instead of running them parallel and that is by attaching attributes to the test classes. Since they are generated anew with every build that doesn't work.

My tests are end-to-end tests and must be run in sequence. How do I do that?

Upvotes: 0

Views: 1106

Answers (1)

Christopher Hamkins
Christopher Hamkins

Reputation: 1639

On the documentation page https://xunit.net/docs/running-tests-in-parallel several ways of preventing parallel execution are described:

Put all test classes into a single test collection by default: [assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)] Default: CollectionBehavior.CollectionPerClass Set the maximum number of threads to use when running test in parallel: [assembly: CollectionBehavior(MaxParallelThreads = n)] Default: number of virtual CPUs in the PC Turn off parallelism inside the assembly: [assembly: CollectionBehavior(DisableTestParallelization = true)] Default: false

These could be done in a class that you manually add once to the test project, so it won't be an autogenerated file that is always newly generated.

For the console runner, there is an option

-parallel none

For the MSBuild runner, you can set the property ParallelizeAssemblies to true or MaxParallelThreads to 1.

Or create / modify an XUnit configuration file for the assembly as described in https://xunit.net/docs/configuration-files#maxParallelThreads.

Upvotes: 2

Related Questions