Scheduling¶
How tests are distributed across workers in the default (--dist load)
mode.
Collection and verification¶
With full collection (the default whenever
lazy collection's auto-default doesn't
pick lazy, which includes small suites and cold caches), every worker
collects the identical session (same args, same ini, same conftest
semantics). Large suites on a warm cache use
lazy collection instead, where each file is collected
on one worker; the rest of this section describes full collection.
Workers verify agreement by item count + hash of the nodeid list; worker
gw0 ships the full list. Divergent collections (typically a randomizing
plugin without a fixed seed) abort the run before any misassignment.
Seeding is barrier-free: each worker starts receiving work the moment its own collection verifies against the reference; early collectors run tests while stragglers finish collecting. The refusal guarantee is per-worker: no worker is ever assigned work before its collection has been cross-checked, so a divergent straggler aborts the run without having received (or misrun) a single test, but tests on already-verified workers may have started by then.
Dispatch order¶
- Slow tests first. Tests with a cached duration ≥ 1s dispatch first,
longest first, one at a time, so they spread across workers instead of
stacking. This is what beats duration-blind schedulers (xdist's default
--dist loadqueues collection-order chunks) on wait-heavy suites: a 54-second test starting at t=0 instead of t=90 changes the whole run's wall time. - Everything else in contiguous chunks. Chunks preserve module locality (module/class fixtures set up once per worker visit) and cut protocol round-trips. Chunk size scales with suite size; refills happen when a worker half-drains.
Costly modules and classes stay on one worker¶
Chunks are small, so a long module is usually spread over several workers,
and each of them sets up its module- and class-scoped fixtures again. rstest
times those fixtures on every run and saves each module's and class's setup
cost to .rstest_cache/scope_costs.json. On the next run, the tests of a
module or class whose setup takes 0.5s or more are sent to one worker as one
unit, so its fixtures are set up once. A costly module groups its classes
too.
A unit is split only when another piece saves more wall time than the setup it repeats, for example a module of long tests that would otherwise leave the rest of the pool idle. Units lead the queue together with the slow tests, biggest first; the rest follows in chunks as above.
The costs ride the shared cache (--cache-push / --cache-pull), so a fresh
CI runner groups from its first run. --doctor lists what was grouped and the
setup time it saved (FIXTURE GROUPING).
This applies to the default --dist load --order throughput on a full
collection. It is off under --shuffle and --order fail-fast (their order
wins) and with one worker. Lazy collection already
hands out whole files.
The duration cache (.rstest_cache/durations.json) is written after every
normal run (not under --dist each or
rstest replay), so the
first run is collection-ordered and every later run is duration-aware.
Fail-fast ordering (--order fail-fast)¶
The order above is --order throughput, the default. --order
fail-fast re-sequences the
--dist load queue for the earliest red signal instead:
- Tests that recently hard-failed, most recent first.
- Tests near code that changed, closest first: a test file edited in the working tree, then a test file changed since the last green run, then a test that reaches a changed source file.
- Flaky tests, most recent flake first.
- Everything else, in throughput order.
Groups 1 and 3 are read from .rstest_cache/flakes.json. Group 2 diffs the
working tree against the last green run's commit (the one
--since-green uses), or against HEAD
when none is recorded, and maps changed source files to tests the way
--changed does: through the coverage map
when it is warm, else the import graph. Non-Python changes are ignored. When
more than a quarter of the run's tests reach a changed source file (a change
to a core module), that tier is dropped, since it no longer singles anything
out. Each leading test is dispatched on its own. At most 128 tests lead, and
quarantined tests (from the --quarantine file or the auto-quarantine set)
are never pulled forward as failed or flaky.
Fail-fast also learns which tests fail together, from the run history
(.rstest_cache/history.msgpack, the last 50 runs). Two tests are partners
when they hard-failed in the same run at least twice and, when the first
fails, the second fails in at least half the runs it was part of. Flaky
reruns, worker crashes and mass failures (more than 100 failures, or more
than 10 that are also over 5% of the run) don't count. Partners change the
order twice:
- Before the run: a leading test that fails along with one ahead of it in at least 80% of runs is the same failure, so it moves behind the other leading tests. The lead then covers distinct failures first.
- During the run: when a test fails, its partners that haven't been
dispatched yet jump to the front of the queue, so the next red from the
same cause shows up next. This matters with
--maxfail Nabove 1, with no limit, and under--watch; with-xthe run stops at the first red.
The history loads on a background thread while workers start, so it adds no
time to the run. With -v, rstest prints what leads the queue, for example
rstest: fail-fast: leading with 2 recent failures, 14 near changes, 3 flaky.
With neither the flag nor [tool.rstest] order set, rstest picks
fail-fast under --watch and throughput otherwise.
Fail-fast has no effect, and an explicit --order fail-fast warns, under:
- an affinity mode (
loadfile/loadscope/loadgroup); --dist each(no dispatch queue);--collect lazy;- a single-worker run (
-n 0/-n 1, or-n autoresolving to one worker), and a passthrough run (-s,--pdb,--co,--debug, ...). For these two the warning fires only when the flag is on the command line, not from config.
Combining an explicit fail-fast with --shuffle is an error.
The nextitem invariant¶
pytest's teardown scoping depends on knowing each test's successor
(nextitem): a worker therefore never runs its last pending item until it
learns what comes next, or learns the queue is exhausted for now
(no_more_items, which runs the held item with nextitem=None). Workers
then keep listening: a failed test from any worker can be rerun on them
until an explicit end-of-session signal confirms every outcome is final.
When -x/--maxfail trips, every worker is told stop_run instead: it
checks for that signal before starting each test, so it finishes only the
test already running and reports its queued items, the held one included,
as never run. Every dispatch path must keep at least one successor in flight or
explicitly release the queue; this invariant shaped most of the
scheduler's edge cases (three deadlocks' worth).
The serial phase¶
@pytest.mark.serial items are excluded from the parallel queue. One
designated worker (the lowest alive one, promoted if it crashes) is held
open; when every other worker's session has
fully finished (fixtures torn down, ports released), the serial items run
there exclusively, in collection order.
Affinity modes¶
--dist loadfile, loadscope, and loadgroup replace the above with
keyed groups in collection order. A dispatch never splits a group, and
duration reordering is off (affinity is the point, at the cost of
long-pole splitting):
loadfile: groups are whole files.loadscope: groups are fixture scopes, a class's tests, or a module's functions.loadgroup: groups are@pytest.mark.xdist_group("name")marks, consolidated across files; unmarked tests stay individual.
Broadcast mode (--dist each)¶
--dist each is not distribution at all: every worker runs the full
suite (xdist --dist=each), so the run legitimately contains each test N
times. Every worker uses the same interpreter, so it checks that the suite
passes repeatedly in isolation and shakes out flakiness; xdist's
heterogeneous --tx environments have no rstest equivalent. There is no item
dispatch queue; each worker
is seeded with every index, and a crash replacement reruns only the dead
worker's remaining items.
Consequences:
- Outcomes are keyed
nodeid [gwN], since the same test appears once per worker. - The duration cache is not written: N× runs would poison LPT (longest-processing-time-first) scheduling on the next normal run.
--rerunsis rejected: rstest reruns a failure on another worker, and with every worker running the full suite that has no meaning.