← Concepts

阅读中文版本

Test Parallelization and Sharding

Parallelization runs independent tests at once to shorten feedback, but shared state and ordering assumptions make unsafe tests flaky.

testing

Definition

Test parallelization is the execution of multiple test cases concurrently, often by sharding across workers, to reduce total wall-clock time of a suite.

Context and scope

It is used in continuous integration where a serial suite would otherwise dominate build duration.

Common misconceptions

  • any test suite can be parallelised without changes
  • running tests in parallel cannot change their results

Anti-patterns

  • sharing a database or global fixture across parallel workers
  • relying on execution order between test cases

Related concepts

Related examples

Authoritative source

https://en.wikipedia.org/wiki/Test_automation

Definition

Test parallelization is the execution of multiple test cases concurrently, often by sharding across workers, to reduce total wall-clock time of a suite.

It is used in continuous integration where a serial suite would otherwise dominate build duration. Compared to simpler alternatives, this concept fits best when the trade-off it represents is the one the system actually cares about.

When to use it

A common use case is applying it at the boundary where the cost of getting it wrong is highest, and the scenario usually involves a concrete guarantee the system must uphold. Unlike an ad hoc approach, it makes that guarantee explicit, reviewable, and testable.

How it works

Independent tests scale cleanly, but tests that share a database, filesystem, or global variable interfere, producing nondeterministic failures that vanish when run alone.

Sharding distributes cases across workers and is safe only when each shard gets isolated state, which usually means per-worker schemas or containers.

Trade-offs and alternatives

Order dependence is the classic smell: a test that passes only after another has run is a hidden coupling that parallel execution exposes. The main drawback is the extra complexity it introduces, so the benefit should clearly outweigh that cost before a team adopts it.

Common misconceptions

A persistent misconception is that any test suite can be parallelised without changes. Another is that running tests in parallel cannot change their results. For example, an engineer who assumes otherwise may design around a guarantee that does not actually exist, which only becomes obvious under load or failure.

Anti-patterns to avoid

Two recurring anti-patterns are to sharing a database or global fixture across parallel workers and to relying on execution order between test cases. Both tend to surface as hard-to-debug production issues rather than obvious test failures. Related concepts include Mock Objects and Behaviour Verification, Golden Master Testing, Test Flakiness Metrics.