Architecture¶
rstest is two programs with a sharp boundary between them:
The Rust orchestrator owns everything around the tests; a vendored pytest core owns everything about the tests.
Why a vendored pytest, not a reimplementation¶
pytest compatibility is not an API; it's ten years of semantics: fixture
finalization order, conftest discovery rules, assertion rewriting, the
exact behavior of importorskip at collection time. Reimplementing that
surface means matching all of it, and every gap shows up as a suite that
behaves differently.
There's a second, harder constraint: plugins import pytest internals.
Many of the most widely used pytest plugins import from _pytest.*, and
plugins check class identity against the real pluggy library. Only the
genuine code satisfies them.
So rstest vendors pytest verbatim (currently 9.2.0) inside
rstest_worker._vendor, shadows it onto sys.path inside worker
processes (never touching a pytest installed in your environment) and
depends on the real pluggy. Plugins load through normal pytest11 entry
points and find exactly the classes they expect.
How a parallel run works¶
- Spawn. N workers start in your project's interpreter, each
announced with an xdist-style identity (
gw0,gw1, ...) that plugins like pytest-django key resources on. - Collect. By default every worker runs identical pytest collection
(same args, same ini, same conftest semantics: this is what keeps
skip/marker behavior exact). Workers verify they collected the same
test set by count and hash; worker
gw0ships the full id list. A large suite on a warm cache switches to lazy collection instead: each file is collected once, on one worker, on demand. - Dispatch. The orchestrator feeds item indices: cached slow tests
first (individually, so they spread across workers), then contiguous
chunks that preserve module-fixture locality. Workers run each test
through pytest's own
runtest_protocol, with correctnextitemteardown semantics. - Stream. Every test phase reports back over the pipe as it happens (progress, failures with captured output, warnings) and the orchestrator renders, merges, and accounts exactly as pytest would.
- Wind down. When the queue empties, workers release their held
items but stay connected: a failed test elsewhere may still be rerun
on them (
--reruns). Once every test's outcome is final, an explicit end-of-session signal lets each worker run its session-fixture finalizers.@pytest.mark.serialtests run only after every other worker has finished its session (exclusively, on a single worker). Then exit codes merge.
The protocol deliberately never rides stdin/stdout: those belong to your
tests (and to pytest itself under -s/--pdb).
Single-worker mode¶
-n 0 and -n 1 skip the scheduling layer entirely: rstest still starts one
Python process in your interpreter, which runs a single pytest session over
your args, and the orchestrator only relays its reports (or hands it the
terminal when a flag such as --pdb or -s needs it). This is the
compatibility anchor, with byte-exact pytest output unless --output picks
rstest's renderer. See
Single-worker mode for the full
guarantee, the flags that force it, and the --reruns exception.