Skip to content

Architecture

rstest is two programs with a sharp boundary between them:

rstest's Rust orchestrator (CLI, scheduling, progress and failures, duration cache, crash recovery, merged reports) talks msgpack over dedicated pipes to worker processes gw0 to gwN, each running your interpreter with rstest_worker and a vendored pytest core.

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

A parallel run in five steps: spawn the workers, collect (full by default, lazy for a large suite on a warm cache), dispatch slow tests and costly modules first, run and stream each phase back, then wind down: held items run, workers stay up for reruns, session fixtures tear down, the serial phase runs, and results merge.

  1. 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.
  2. 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 gw0 ships 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.
  3. 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 correct nextitem teardown semantics.
  4. 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.
  5. 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.serial tests 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.