pytest-xdist
raw JSON → 3.8.0 verified Tue May 12 auth: no python install: stale quickstart: stale
The pytest-xdist plugin extends pytest with new test execution modes, primarily designed for distributing tests across multiple CPUs or hosts to significantly speed up test execution. It also offers features like running tests in a Python subprocess and formerly supported remote SSH execution and `--looponfail` mode. Currently at version 3.8.0, the library is actively maintained with regular feature updates and bug fixes.
pip install pytest-xdist Common errors
error pytest: error: unrecognized arguments: -n auto ↓
cause The `pytest-xdist` plugin is not installed, so pytest does not recognize its command-line options like `-n` or `--dist`.
fix
pip install pytest-xdist
error pytest: error: unrecognized arguments: --looponfail ↓
cause The `--looponfail` feature, which automatically re-runs failing tests, was removed in `pytest-xdist` version 3.0.0.
fix
Remove
--looponfail from your pytest command. Consider using alternative tools like pytest-watch for similar functionality. error pytest: error: unrecognized arguments: --tx ssh=user@host:python=python3 ↓
cause Remote execution via the `--tx` option was removed in `pytest-xdist` version 3.0.0.
fix
Remove the
--tx option and explore alternative tools or methods for remote test execution. error ERROR: Package 'pytest-xdist' requires a different Python: X.Y.Z < 3.8 ↓
cause The Python version installed on your system is older than 3.8, which is the minimum requirement for `pytest-xdist`.
fix
Upgrade your Python installation to version 3.8 or newer to meet the library's requirements.
Warnings
gotcha The `-s` or `--capture=no` option for disabling pytest's output capture does not work with `pytest-xdist`. This means you will not see real-time stdout/stderr output from workers, and debugging with tools like `PDB` is generally not supported for tests running in parallel workers. ↓
fix For debugging, run tests without `-n` (i.e., sequentially). For inspecting output, rely on logging or `--capture=fd` (file descriptor capture) where available and aggregated at the end of the run.
gotcha Tests using `pytest.mark.parametrize` with unordered iterables (e.g., `set`) can lead to inconsistent test collection order across workers, causing `pytest-xdist` to fail. ↓
fix Ensure parameterized values have a consistent order by converting them to lists or sorting them (e.g., `list({'a', 'b'})` or `sorted({'a', 'b'})`).
deprecated The `--looponfail` and `--rsyncdir` command-line arguments and their corresponding configuration variables (`looponfailroots`, `rsyncdirs`) are deprecated. They are scheduled for removal in `pytest-xdist 4.0`. ↓
fix Migrate away from using `--looponfail` (which is already deprecated). For `--rsyncdir`, consider alternative methods for syncing code to remote environments, as modern CI/CD practices often handle this differently. The `--boxed` argument was also deprecated and removed in 3.0.0, use `pytest-forked` and `--forked` instead.
gotcha pytest's `scope='session'` fixtures are executed once per *worker process*, not once for the entire `pytest-xdist` run across all workers. This can lead to unexpected resource setup/teardown behavior and state issues if not managed carefully. ↓
fix For fixtures that truly need to run once per entire test session across all workers, explicit inter-process communication or shared storage (e.g., temporary files) is required, often with a 'first worker initializes, others wait' pattern. Consider using plugins like `pytest-shared-session-scope`.
breaking Users have reported that `--exitfirst` (`-x`) (which stops the test session on the first failure) does not function as expected with `pytest-xdist` versions 3.4.0 and higher. Tests might continue running in other workers despite a failure. ↓
fix If strict `--exitfirst` behavior is critical for your workflow, you might need to pin `pytest-xdist` to version `3.3.1` or earlier. Alternatively, check the latest `pytest-xdist` documentation or issue tracker for updates or workarounds regarding this behavior.
gotcha A test run resulted in a 'TIMEOUT'. This can be caused by various factors, including an individual test hanging indefinitely (e.g., due to an infinite loop, a deadlock, or waiting for an unresponsive external resource), or the overall test suite exceeding the configured maximum execution time in the CI environment. ↓
fix Investigate logs for the specific tests running at the time of the timeout to identify the hanging test or process. Consider adding more granular timeouts at the test or fixture level (e.g., using `pytest-timeout`) if a specific test is the culprit. Review system resource utilization during the test run if the entire environment becomes unresponsive. Ensure external services or resources accessed by tests are stable and responsive.
Install
pip install pytest-xdist[psutil] Install compatibility stale last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) pytest-xdist wheel - - 31.3M
3.10 alpine (musl) psutil wheel - - 32.1M
3.10 alpine (musl) pytest-xdist - - - -
3.10 alpine (musl) psutil - - - -
3.10 slim (glibc) pytest-xdist wheel 2.7s - 32M
3.10 slim (glibc) psutil wheel 3.0s - 33M
3.10 slim (glibc) pytest-xdist - - - -
3.10 slim (glibc) psutil - - - -
3.11 alpine (musl) pytest-xdist wheel - - 34.4M
3.11 alpine (musl) psutil wheel - - 35.3M
3.11 alpine (musl) pytest-xdist - - - -
3.11 alpine (musl) psutil - - - -
3.11 slim (glibc) pytest-xdist wheel 2.8s - 35M
3.11 slim (glibc) psutil wheel 3.0s - 36M
3.11 slim (glibc) pytest-xdist - - - -
3.11 slim (glibc) psutil - - - -
3.12 alpine (musl) pytest-xdist wheel - - 26.0M
3.12 alpine (musl) psutil wheel - - 26.9M
3.12 alpine (musl) pytest-xdist - - - -
3.12 alpine (musl) psutil - - - -
3.12 slim (glibc) pytest-xdist wheel 2.6s - 26M
3.12 slim (glibc) psutil wheel 2.9s - 27M
3.12 slim (glibc) pytest-xdist - - - -
3.12 slim (glibc) psutil - - - -
3.13 alpine (musl) pytest-xdist wheel - - 25.8M
3.13 alpine (musl) psutil wheel - - 26.6M
3.13 alpine (musl) pytest-xdist - - - -
3.13 alpine (musl) psutil - - - -
3.13 slim (glibc) pytest-xdist wheel 2.7s - 26M
3.13 slim (glibc) psutil wheel 2.9s - 27M
3.13 slim (glibc) pytest-xdist - - - -
3.13 slim (glibc) psutil - - - -
3.9 alpine (musl) pytest-xdist wheel - - 30.7M
3.9 alpine (musl) psutil wheel - - 31.4M
3.9 alpine (musl) pytest-xdist - - - -
3.9 alpine (musl) psutil - - - -
3.9 slim (glibc) pytest-xdist wheel 3.3s - 31M
3.9 slim (glibc) psutil wheel 3.6s - 32M
3.9 slim (glibc) pytest-xdist - - - -
3.9 slim (glibc) psutil - - - -
Imports
- pytest-xdist
pytest -n auto
Quickstart stale last tested: 2026-04-24
# Assuming you have pytest and pytest-xdist installed and tests in a 'tests/' directory
# Create a simple test file, e.g., tests/test_example.py
# import time
# def test_fast():
# assert True
# def test_medium():
# time.sleep(0.5)
# assert True
# def test_slow():
# time.sleep(1)
# assert True
import os
import subprocess
def run_pytest_xdist(num_processes):
cmd = ['pytest', '-n', str(num_processes)]
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode
# Example usage: run tests on all available CPU cores
# A real-world scenario would just be `pytest -n auto` from the command line
if __name__ == '__main__':
# Ensure pytest is installed for this example to run locally in a script
try:
import pytest
except ImportError:
print("pytest is not installed. Please install with 'pip install pytest'.")
exit(1)
print("\n--- Running tests with pytest -n auto ---")
# In a real shell, you would simply type: pytest -n auto
# For programmatic execution, we demonstrate the subprocess call
exit_code = run_pytest_xdist('auto')
if exit_code == 0:
print("Tests passed successfully with -n auto.")
else:
print(f"Tests failed with -n auto. Exit code: {exit_code}")
print("\n--- Running tests with pytest -n 2 ---")
# In a real shell, you would simply type: pytest -n 2
exit_code = run_pytest_xdist(2)
if exit_code == 0:
print("Tests passed successfully with -n 2.")
else:
print(f"Tests failed with -n 2. Exit code: {exit_code}")