Run py.test test in different process
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running pytest tests in separate processes is useful for isolation, speed, and stability, especially when tests mutate global state or use CPU-heavy workloads. The most common approach is pytest-xdist, but subprocess execution can also be useful for targeted process boundaries. This guide shows practical options and when to use each.
Core Topic Sections
Why run tests in separate processes
Process-level separation helps with:
- Isolation from shared module state.
- Better CPU utilization on multi-core machines.
- Containment of crashes in one worker process.
It does not automatically solve flaky tests caused by timing or external dependencies, so test design still matters.
Option 1: use pytest-xdist for parallel workers
Install plugin:
Run with automatic worker count:
Or fixed count:
This is the standard way to distribute test cases across multiple processes.
Control test distribution strategy
For uneven test durations, distribution strategy affects total runtime.
Examples:
loadscope keeps tests from same module or class together, which can reduce fixture setup duplication.
Option 2: run one test in a dedicated subprocess
Sometimes you need explicit isolation for one problematic test or fixture chain.
This pattern is useful in custom CI orchestration or debugging harnesses.
Use markers to separate process-sensitive tests
Some tests should run serially or with dedicated environment due to shared resources. Use markers and split commands.
pytest.ini example:
Run isolated group first or last:
This keeps parallelism high while containing risky tests.
Avoid resource collisions in parallel mode
Parallel processes can collide on:
- Temp files with fixed names.
- Shared databases.
- Network ports.
Use per-worker resources:
Per-worker partitioning removes many nondeterministic failures.
Coverage with multiple processes
Coverage works with parallel test execution when configured properly.
If you run separate subprocess commands, ensure coverage combine is done in CI workflow.
Debugging failures in parallel runs
A practical debugging approach:
- Re-run failing test serially.
- Re-run with same seed or deterministic env.
- Compare behavior under
-n 1and-n auto.
This helps distinguish race issues from functional failures.
CI recommendations
In CI, choose worker count relative to available cores and memory. Too many workers can increase context-switch overhead or cause resource starvation.
Start conservative, measure runtime and flake rate, then tune gradually.
Common Pitfalls
- Enabling parallel workers without isolating shared filesystem or port resources.
- Assuming process isolation eliminates all test flakiness automatically.
- Running too many workers and degrading performance due to overhead.
- Mixing serial-only fixtures with parallel execution without markers.
- Forgetting coverage aggregation when tests run in separate subprocesses.
Summary
- Running pytest in different processes improves isolation and often speed.
- '
pytest-xdistis the best default for parallel process execution.' - Use dedicated subprocess runs for targeted isolation scenarios.
- Separate serial-only tests with markers and resource partitioning.
- Tune worker count and validate reliability, not only raw execution speed.

