To run pytest tests in parallel, install the pytest-xdist plugin and add -n auto to your command. pytest then starts one worker process per CPU core and spreads the tests across them, so a suite that takes four minutes on one core can finish in a fraction of that time. The plugin is the standard answer for pytest xdist parallel runs and works with Python 3.13 and 3.14 and current pytest 8.x.
Parallel runs only help when tests are independent. The main work is making sure tests do not share files, databases or ports, and knowing how xdist hands tests to workers.
Install the plugin into the same environment as pytest:
Then run the suite with a worker count:
-n auto starts as many workers as the plugin detects physical CPU cores. You can also pass a number such as -n 4, or -n logical to count logical cores (hyperthreads) as well. -n 0 turns xdist off, which is useful when you want a normal single-process run for debugging with a debugger or print output.
Each worker is a separate Python process that imports your test modules and runs the tests it is given. The controller process collects results and prints one combined report. If collection differs between workers (for example because a test id is generated randomly), xdist stops with an error, so keep parametrized ids deterministic.
The --dist option controls how tests are assigned:
| Mode | Behaviour | Use it when |
|---|---|---|
load (default with -n) | Sends tests to whichever worker is free, in small batches | Tests are independent |
loadscope | Keeps all tests of one module or class on the same worker | Tests in a class share expensive setup |
loadfile | Keeps all tests of one file on the same worker | A file shares a resource |
loadgroup | Keeps tests with the same xdist_group mark together | You need named groups |
worksteal | Starts with an even split, then idle workers steal tests | Test durations vary a lot |
To pin related tests to the same worker, mark them and use loadgroup:
Groups keep tests on one worker, but they do not run in a guaranteed order. If a test needs another test to run first, that is a design problem to fix rather than a scheduling one.
Tests that pass alone and fail under -n auto almost always share state. Common causes:
/tmp/output.json that several tests write.Use pytest's built-in tmp_path fixture for files. Every test gets its own directory, so workers never collide:
For a database, give each worker its own database or schema. The worker_id fixture returns "gw0", "gw1" and so on, or "master" when xdist is not active:
You can read the same value from the PYTEST_XDIST_WORKER environment variable, and PYTEST_XDIST_WORKER_COUNT tells you how many workers are running.
A session-scoped fixture runs once per worker, not once per run. If setup must happen only once (for example downloading test data), use a lock file so the first worker does the work and the others reuse it:
tmp_path_factory.getbasetemp().parent is the directory shared by all workers of one run, while the base temp directory itself is per worker. The filelock package is a separate install and is already a dependency of many tools.
pytest-cov supports xdist and combines coverage data from all workers, so pytest -n auto --cov=src works without extra configuration. Output from print is captured per test as usual; add -s only when debugging and prefer -n 0 for that.
To find which tests are slow and decide whether parallelism pays off, run:
Starting workers costs time: each one imports your project and plugins. For a suite that already runs in two or three seconds, -n auto can be slower than a plain run. Parallelism pays off for suites that run for tens of seconds or more.
You can make the option permanent in pyproject.toml:
With this setting, use -n 0 on the command line to switch it off for one run.
Start with -n auto. On a CI machine with limited memory, set a number explicitly, because every worker holds its own copy of your imported code and fixtures.
It shares state with another test, such as a file, database row or port. Run the failing test alone, then with -n 2, and replace shared resources with per-test or per-worker ones.
Older xdist versions had a remote execution mode, but it has been removed. For multiple machines, split the test files across CI jobs instead and run xdist inside each job.
No. Distribution depends on which worker is free, so order can change between runs. Tests must not depend on order.
Install pytest-xdist and run pytest -n auto to spread tests over CPU cores. Choose --dist to control grouping, use tmp_path and per-worker resources such as the worker_id fixture to avoid collisions, and guard one-time setup with a file lock. Keep -n 0 for debugging and measure first, because short suites may not benefit.