NUnit can be used for a variety of test approaches, including writing unit tests and integration tests. Integration tests may take a non-trivial amount of time to complete, and so a large suite can lead to long overall test run times and delayed feedback of any bugs or issues. Situations like these may benefit from splitting up the test run into multiple “chunks” or shards, where each targets a subset of the total suite.
Overview and Usage
NUnit shards tests by partitioning them up into repeatable, deterministic buckets. The number of buckets and the bucket to target within a given run can be specified on the command line using the “partition” filter. For example, the following will allow dotnet test to split a run into 3 shards and target only the first:
dotnet test -- NUnit.Where="partition == 1/3"
This pattern can then allow for a large suite to be split up across multiple nodes by having each target a separate shard:
dotnet test -- NUnit.Where="partition == 1/3"dotnet test -- NUnit.Where="partition == 2/3"dotnet test -- NUnit.Where="partition == 3/3"
NUnit Console also supports this:
nunit3-console {Tests.dll} --where="partition == 1/3"nunit3-console {Tests.dll} --where="partition == 2/3"nunit3-console {Tests.dll} --where="partition == 3/3"
This sharding, if done in parallel, can reduce total suite run times to a half, third, or more depending on the number of parallel shards. This drastically shortens feedback loops and allows for quicker responses to any potential bugs or issues.
Support
Built-in support for test sharding was added in version 3.14.0 of the NUnit framework. If running the tests using dotnet test then version 4.6 or higher of the NUnit3TestAdapter is required. If running the tests using the NUnit console then version 3.17 or higher of the console runner is required.
