xdist support matrix¶
How each pytest-xdist flag, hook and fixture maps onto rstest, for teams moving an xdist suite across.
Who this is for¶
You are moving a suite off pytest-xdist and need one lookup: for each xdist flag, hook, and worker-identity fixture, does rstest support it, emulate it, or drop it? This page answers that and links to the deeper treatment of each item.
- The contract. At
-n 0, rstest runs in single-worker mode: one vendored-pytest session whose per-test outcomes match pytest exactly, and any difference is a bug (see Compatibility).-n 1takes the same path unless--rerunsturns it into a one-worker rerun pool. In that mode rstest itself handles only the few flags it shares with pytest or a plugin. - Replace, not wrap. rstest replaces xdist rather than wrapping it. The
worker environment is xdist-shaped on purpose, so plugins keep working,
and the worker-identity fixtures (
worker_id,testrun_uid) are provided natively. - Where the risk is. Migration risk sits in two areas: flags that silently no-op, and hooks that run per worker instead of once.
For the narrative version see Migrating from pytest-xdist.
Flag matrix¶
| pytest-xdist flag | rstest equivalent | Status / notes |
|---|---|---|
-n <N> |
-n <N> |
Same. |
-n auto |
-n auto (differs) |
rstest's auto is the logical core count (as reported by the OS, which honors CPU affinity and, on Linux, a cgroup CPU quota), then capped by the selected work (test files on a cold cache, tests once timings are cached) and by the cached suite time; it is the default. xdist's auto is the physical core count when psutil is installed. On a machine with SMT the two can differ by 2x: while comparing the runners, pin -n to the count your xdist job used. PYTEST_XDIST_AUTO_NUM_WORKERS is ignored: pass -n or set [tool.rstest] numprocesses. |
-n logical |
none | rstest parse error, exit 2 (invalid value 'logical' for '--numprocesses <NUMPROCESSES>': expected a non-negative integer or "auto"). Use -n auto or an explicit number. |
-n 1 |
-n 1 (differs) |
xdist's -n 1 runs one gw0 worker with workerinput; rstest's -n 1 (like -n 0) is plain single-worker mode with no worker identity. |
--dist load |
--dist load (default) |
Same, plus duration-aware scheduling. When auto picks lazy collection (large suite, warm cache), files are dispatched whole unless --dist load is given explicitly. |
--dist loadfile |
--dist loadfile |
Same. File affinity, in-file order. |
--dist loadscope / loadgroup |
same names | Supported, incl. @pytest.mark.xdist_group; rejected under --collect lazy (needs full collection), and auto never picks lazy for them. See --dist. |
--dist each |
--dist each (partial) |
Full suite per worker, but every worker uses the same interpreter. Heterogeneous --tx gateways have no equivalent. --reruns rejected in this mode, and so is --collect lazy. |
--dist no / --dist=no |
none | rstest error, exit 1 (no is not a valid --dist mode); single-worker mode is -n 0. |
--dist worksteal |
none | rstest error, exit 1 (unknown --dist mode: worksteal). Use --dist load (the default). It doesn't steal work either; it balances by dispatching tests with a cached duration of 1s or more first, longest first, one at a time (see Scheduling). |
-p no:xdist |
-p no:xdist |
Forwarded to the vendored pytest, which then doesn't load pytest-xdist. rstest's own parallelism and worker_id / testrun_uid fixtures are unaffected. But a leftover -n in addopts is then an unknown option: the run exits 4 (unrecognized arguments: -n). Remove -n from addopts before disabling or uninstalling xdist. |
-d |
--dist load |
-d is xdist's load-balancing shorthand, which is rstest's default. Forwarded verbatim (see below), no effect. |
--maxprocesses |
none | Use -n (no separate cap). Forwarded verbatim, no effect. |
--max-worker-restart |
none | No equivalent: rstest auto-respawns crashed workers on a fixed, non-tunable budget (see Crash handling). Forwarded verbatim, no effect. |
--tx (gateways) |
none | No equivalent: one local interpreter. --dist each covers same-env broadcast, not heterogeneous environments. |
--rsyncdir / --rsync |
none | No equivalent: rstest runs local workers, no remote sync. |
-p xdist.looponfail / --looponfail |
--watch |
With import-graph selection. See --watch. On rstest's command line, --looponfail / -f is refused (exit 1) with a pointer to --watch; from ini addopts it is switched off inside the workers. |
What happens to an unsupported xdist flag? --dist no/--dist=no and --dist worksteal are consumed by rstest's own --dist and rejected as invalid modes (exit 1); -n logical is rejected by the
argument parser (exit 2). --looponfail / -f is refused on the command line and switched off from addopts (see its row above). The rest (--tx, --rsync*, -d, --max-worker-restart, --maxprocesses) are forwarded to the vendored pytest session verbatim, so the outcome depends on whether pytest-xdist is installed:
- pytest-xdist installed (usual mid-migration): the flag parses but has no effect, because rstest keeps xdist's session inert (
dist = no) and nothing acts on it. Silently ignored, no error, no warning. - pytest-xdist not installed: pytest doesn't recognize the option, so the vendored core exits with a usage error (exit 4).
Either way these flags don't do anything under rstest; remove them from addopts once no pytest-xdist job still depends on them (see the staged rollout). If addopts = -n 4 with pytest-xdist installed is still in your ini, it is neutralized inside rstest workers automatically (options parse, the xdist session never engages, no nested workers). Remove it at your convenience and pass -n to rstest. With pytest-xdist uninstalled or disabled (-p no:xdist), the same addopts line fails the run with exit 4.
Before uninstalling pytest-xdist, run rstest xdist-removal-check. It finds the leftovers described above (-n and other xdist flags in addopts, import xdist, xdist hooks, required_plugins) and exits 1 while anything still blocks the removal.
Hook matrix¶
xdist's controller-side ("master") hooks fire in the controller process around each worker. rstest has no separate controller process, so the supported ones are emulated: each worker plays controller for itself, calling your implementation against a node shim with its own workerinput, gateway.id, and config. Full semantics: xdist hook emulation.
| xdist hook | Status |
|---|---|
pytest_configure_node(node) |
Emulated. Runs per worker; hooks that are pure functions of the node (read gateway.id, fill workerinput, provision from them, such as SQLAlchemy's follower_ident) produce the same observable result as xdist. |
pytest_testnodeready(node) |
Emulated. Runs per worker against the node shim. |
pytest_testnodedown(node, error) |
Emulated. Fires at session finish on the normal path; a crashed worker's pytest_testnodedown runs on a surviving worker, so teardown must be a function of node.workerinput alone. |
pytest_xdist_make_scheduler |
Not emulated (never called). There is no custom-scheduler plug point: --dist is a fixed set of modes with no override. |
pytest_xdist_auto_num_workers |
Not emulated (never called). The worker count is rstest's decision. |
pytest_xdist_newgateway |
Not emulated (never called). There are no execnet gateways. |
pytest_xdist_setupnodes |
Not emulated (never called). |
pytest_xdist_node_collection_finished |
Not emulated (never called). There is no controller collection phase. |
pytest_handlecrashitem |
Not emulated (never called). Crash handling lives in the Rust orchestrator. |
pytest_xdist_rsyncstart / pytest_xdist_rsyncfinish |
Not emulated (never called). There is no rsync to remote gateways. |
pytest_xdist_getremotemodule |
Not emulated (never called). There are no remote execnet workers. |
The same list, with the reasons, is in Plugins: hook coverage.
Two structural caveats on the three emulated hooks: they run N times concurrently in N processes (controller-side shared state such as counters, registries, and pools needs rework; derive everything from gateway.id or a uuid), and crashed-node teardown runs on a survivor that never saw the dead node's configure_node. Every other conftest hook (pytest_configure, pytest_collection_modifyitems, pytest_sessionstart/finish, pytest_runtest_*) also runs inside each worker (the same model as xdist), so make shared-state hooks idempotent or key them on workerinput["workerid"] / PYTEST_XDIST_WORKER (set under both xdist and rstest; RSTEST_WORKER_ID is the rstest-only equivalent). Under lazy collection, which auto picks for large warm-cache runs, pytest_collection_modifyitems sees only the files that worker collects; pin --collect full if a hook needs the whole suite.
Fixtures & worker identity¶
rstest provides native worker_id and testrun_uid fixtures, with pytest-xdist's semantics, so def test(worker_id): ... resolves whether or not pytest-xdist is installed. Removing pytest-xdist from your config does not lose them.
worker_id: the worker the test runs on,gw0,gw1, ..., or"master"below-n 2(single-worker mode, no worker identity).testrun_uid: one uid shared by every worker in a run. Below-n 2a fresh uid is generated per session, matching xdist's standalone behavior.
When pytest-xdist is also installed it defines the same two fixtures. rstest's definitions take precedence over xdist's (a conftest or test-module fixture of the same name still overrides both), and in a pool of two or more workers the two definitions return identical values anyway.
Under the fixtures, rstest sets the full xdist-compatible surface on every pool worker, for plugins and conftests that read it directly:
RSTEST_WORKER_IDenv var, thegwNvalue, rstest-specific.PYTEST_XDIST_WORKER,PYTEST_XDIST_WORKER_COUNTandPYTEST_XDIST_TESTRUNUIDenv vars, set so environment-grepping plugins/conftests keep working.config.workerinput, which carriesworkerid(gwN),workercount, the run uid under bothtestrunuid(xdist's key) andtestrun_uid(one uid per run, shared by all workers),randomly_seed(pytest-randomly) andrandom_order_seed(pytest-random-order), one run-level value shared by all workers,mainargv, and thecov_master_*keys pytest-cov expects. Two more are seeded only when the plugin is installed and pytest-xdist is not:server_port(pytest-retry) andmypy_config_stash_serialized(pytest-mypy).
Reading the surface directly, instead of via the fixtures, also works:
Values at -n 0 / -n 1. There is no workerinput below -n 2: config.workerinput does not exist, RSTEST_WORKER_ID, PYTEST_XDIST_WORKER, PYTEST_XDIST_WORKER_COUNT and PYTEST_XDIST_TESTRUNUID are unset, the worker_id fixture returns "master", and testrun_uid returns a fresh per-session uid, with or without pytest-xdist installed. This differs from xdist's -n 1, which does create a gw0 worker with workerinput.
The exception is --reruns at -n 0/1, which runs a one-worker rerun pool. There config.workerinput exists (workerid gw0, workercount 1), RSTEST_WORKER_ID=gw0, RSTEST_WORKER_COUNT=1, PYTEST_XDIST_WORKER=gw0, PYTEST_XDIST_WORKER_COUNT=1 and PYTEST_XDIST_TESTRUNUID are set, and xdist's helpers get_xdist_worker_id() / is_xdist_worker() report gw0, yet the fixtures still return "master" and a fresh uid. Code that reads config.workerinput or the environment directly (rather than via the fixtures) must guard for this case too. pytest-django, for one, then names its test database test_<name>_gw0 instead of test_<name>, which matters with --reuse-db: a database kept by a plain -n 0 run is not the one a --reruns run reuses, and vice versa.
Plugins keyed on worker identity work unchanged: pytest-django's per-worker test databases are the canonical case (SQLite only in the corpus; check a Postgres or MySQL setup on your own suite). And hypothesis's shared example DB under many workers can be split per worker with DirectoryBasedExampleDatabase(f".hypothesis/{os.environ.get('PYTEST_XDIST_WORKER', 'master')}") (see Compatibility).
Known divergences¶
These are not rstest bugs: they are catalogued differences and design choices. Full catalogue: Parity divergences.
- Scheduling / run order. At
-n ≥ 2, the orderpytest_collection_modifyitemsimposes is only where dispatch starts, not a guarantee: cached slow tests go first and tests on different workers run concurrently (details). Suites that need strict ordering need-n 0or an affinity--distmode. - Crash cleanup is weaker than xdist's. xdist's controller is a separate always-alive process; rstest needs a surviving worker to run a dead node's
pytest_testnodedown. If the last worker crashes, cleanup is skipped and rstest warnsrstest: no surviving worker to run pytest_testnodedown for a crashed worker; per-worker resources may leak. The replacement worker can also race the survivor's drop; use uuid-based idents (as SQLAlchemy does) and the race disappears. See xdist hook emulation. - Controller-side shared services aren't emulated. A plugin needing one service shared across the whole pool has no central controller to host it; known cases (pytest-retry, pytest-rerunfailures) are handled per worker or neutralized in favor of native
--reruns. See xdist hooks. - Unstable parametrize IDs block the pool. If workers collect different ids, rstest refuses to dispatch, the same constraint as xdist, with no automatic fallback. See Unstable parametrize ids.
Go deeper¶
- Migrating from pytest-xdist: the full flag map, what your plugins see, and what improves.
- xdist hook emulation: how the three node hooks are emulated,
numprocessesvisibility, and per-worker conftest semantics. - Compatibility: the
-n 0byte-exact contract, the measured battery, and the known-gaps table (incl. xdist controller-side hooks). - Parity divergences: every catalogued reason a suite diverges and the upstream fix.
- Markers:
@pytest.mark.serial,@pytest.mark.flaky,@pytest.mark.xdist_group. - CLI:
-n,--dist, and the full forwarded-flag surface.