Run your existing suite¶
New to rstest and only evaluating? rstest try
compares a pytest run and an rstest run of your suite in one command.
Run rstest from your project root, exactly where you would run pytest:
No other arguments needed: rstest honors your project's pytest configuration
(pytest.toml, .pytest.toml, pytest.ini, .pytest.ini, pyproject.toml,
tox.ini, or setup.cfg, including
testpaths, addopts, python_files, and markers) because collection
runs through a vendored pytest core.
On your own terminal you'll see the bar view: a ✓/✗ line per
test, inline failures, and a live footer showing overall progress with an
ETA and, per worker, which test is running and for how long (idle when it
has nothing). Here is a small four-test file at -n 2, caught mid-run:
rstest 0.12.0 — 2 workers (parallel by default; -n 0 for single-worker mode)
[gw0] ✓ tests/test_math.py::test_add 0.20s [ 25%]
[gw1] ✓ tests/test_math.py::test_add_zero 0.21s [ 50%]
[gw1] s tests/test_math.py::test_skipped [ 75%]
██████████████████████░░░░░░░░ 75% (3/4) ~0s left
gw0 0.0s tests/test_math.py::test_add_negative
gw1 idle
When the run ends the footer is replaced by the result bar (green, red, and yellow segments for passed, failed, and skipped or xfail) and pytest's summary line:
[gw0] ✓ tests/test_math.py::test_add_negative 0.21s [100%]
Results (0.61s):
██████████████████████████████ 4/4
3 passed, 1 skipped in 0.61s
In CI, or whenever output is piped, you get the compact dots view instead
(Reading the output). If the first run fails before
any test runs (an interpreter or plugin error), see
Troubleshooting; if tests fail only under
rstest, see When something fails.
Reading the output¶
rstest picks the output style from where the output goes:
baron an interactive terminal: the pytest-sugar-style view shown above.dotswhen output is piped or running in CI, so logs stay stable: one character per test, pytest's classic progress view.
Here is dots on django-allauth's 2,050-test suite at -n 4 (the middle
lines are elided). Benchmarks measures this
suite at 8.5s at -n 4:
$ rstest -n 4
rstest 0.12.0 — 4 workers (parallel by default; -n 0 for single-worker mode)
........................................................................ [ 3%]
..............................s......................................... [ 7%]
[... 25 more lines ...]
..................................................s..................... [ 98%]
.................................. [100%]
2048 passed, 2 skipped in 8.02s
Pick a style explicitly with --output: dots, verbose or bar for
terminals, github, gitlab, buildkite, teamcity or azure for CI
annotations, tap or json for machine-readable streams (see the
CLI reference). The rest of this page uses dots,
since it is stable to show. On a single worker with no --output set, rstest
prints pytest's own terminal output instead
(see below).
What each part of a run tells you:
- The header line states the worker count. rstest is parallel by default; this line is the visible reminder.
- Dots stream live as tests finish across all workers:
.pass,Ffail,sskip,xxfail,Xxpass,Eerror, pytest's vocabulary. - Failures print with full pytest-style tracebacks (assertion rewriting
included) and captured stdout/stderr/log sections, in the
--tbstyle you pass:--tb=lineprints pytest's onepath:line: messageline per failure (after its captured output, as pytest does) and--tb=noprints no failure block at all. - The summary line uses pytest's accounting: the counts match what
pytest would print for the same run, in pytest's order, including
warnings, the
deselectedcount, and a teardown error counted as an error on top of the test's call outcome.
The live footer described above belongs to the bar view on a terminal;
it is disabled automatically when output is piped, in CI (a CI variable,
even on a pty), and when color is off (NO_COLOR, --color=no,
TERM=dumb), which then means no escape sequences at all. It is what
makes long-running tests visible the moment they start, not after they
finish.
Add -v for pytest's classic PASSED/FAILED lines. In parallel mode each line is prefixed with
the worker that ran it (gw0, gw1, ...), and lines interleave as workers
finish:
$ rstest -n 2 -v
rstest 0.12.0 — 2 workers (parallel by default; -n 0 for single-worker mode)
[gw0] tests/test_math.py::test_add PASSED [ 16%]
[gw1] tests/test_math.py::test_add_zero PASSED [ 33%]
[gw1] tests/test_math.py::test_skipped SKIPPED [ 50%]
[gw0] tests/test_math.py::test_add_negative PASSED [ 66%]
[gw1] tests/test_login.py::test_logout PASSED [ 83%]
[gw0] tests/test_login.py::test_session FAILED [100%]
The same [gwN] attribution appears in the failure summary, so you can
see which worker hit each failure:
--- FAILED [gw0] tests/test_login.py::test_session ---
def test_session():
status = 401
> assert status == 200
E assert 401 == 200
tests/test_login.py:3: AssertionError
At -n 0/-n 1 there is no worker, so there is no prefix: the single
pytest session prints its own -v output, exactly as pytest does
(--output verbose gives you rstest's own verbose view instead):
$ rstest -n 0 -v
============================= test session starts ==============================
platform darwin -- Python 3.13.13, pytest-9.2.0, pluggy-1.7.0 -- /path/to/.venv/bin/python
cachedir: .pytest_cache
rootdir: /path/to/project
collecting ... collected 6 items
tests/test_math.py::test_add PASSED [ 16%]
tests/test_math.py::test_add_zero PASSED [ 33%]
...
Selecting tests¶
Everything you know from pytest works unchanged:
$ rstest tests/test_login.py # one file
$ rstest tests/test_login.py::TestLogin # one class
$ rstest -k "login and not slow" # keyword filter
$ rstest -m integration # marker filter
$ rstest --lf # only last failures
$ rstest -x # stop at first failure (globally)
rstest adds selectors of its own (not pytest flags):
$ rstest --changed # only tests affected by your edits
$ rstest --since-green # only what changed since the last all-green run
$ rstest --incremental # skip tests that passed and whose code is unchanged
--changed runs just the tests a change can reach, using the per-test
coverage index when it is warm and the import graph otherwise; for gating CI
use --changed-strict, which runs
everything when it can't connect a change. --since-green does the same
against the commit of the last all-passing run. Both need a git checkout.
--incremental needs no git, but it does
need the coverage index.
The coverage index records which tests executed each source line. Build it with one coverage run that keeps per-test contexts (this needs pytest-cov installed):
Without it, --changed still works from the import graph, and
--incremental has nothing to skip, so it runs every test. Keep --cov=. on
--incremental runs so the index stays current. More in
Selecting changed tests and
Per-test contexts;
see Watch mode for the on-save version.
Controlling parallelism¶
$ rstest -n 4 # four workers
$ rstest -n auto # the default: logical cores, capped for small suites
$ rstest -n 0 # single-worker mode: one pytest session (same as -n 1)
$ rstest -n 1 # identical to -n 0
How -n auto sizes the pool: it starts from your logical core count and
never starts more workers than the work you selected. On a cold cache that
means one worker per selected test file (one selected test runs one
session). Once timings are cached it counts tests instead of files, and also
caps at about one worker per 2 seconds of cached test time, since worker
startup isn't worth it for a sub-second run. So a tiny suite runs on one or
two workers while a slow single file still spreads across workers. It only
ever caps downward; pass an explicit -n to override.
-n 0 and -n 1 are the compatibility escape hatch, single-worker mode:
one pytest session with pytest's own behavior and output
(what it guarantees).
Commit your defaults to [tool.rstest] in pyproject.toml so you don't
retype flags:
Command-line flags override these; full key list in CLI → Configuration file.
When to drop to -n 0
Under ~10 seconds of serial runtime, parallelism rarely pays: worker
startup amortizes poorly and -n auto already caps itself low on small
suites. Reach for -n 0 deliberately when you want pytest's exact
behavior: reproducing a difference from pytest, or running a suite
whose tests depend on order across files (if the dependency is only
within a file, --dist loadfile keeps each file on one worker).
Above ~10s, let -n auto parallelize. The win grows with suite size
and is largest for wait-heavy suites: run --doctor if you're unsure where your time goes.
Two runs make it faster¶
rstest records per-test durations in .rstest_cache/. From the second run
on, the scheduler starts your slowest tests first, which is what keeps
workers busy at the end of the run instead of waiting on one long test.
On wait-heavy suites this is dramatic: aiohttp's suite more than halves
between its cold and warm runs (150s to 67s; see Benchmarks).
What rstest writes to disk¶
A run leaves two directories in your project root. Add both to
.gitignore; both are safe to delete at any time.
.rstest_cache/is rstest's own: test durations for scheduling, the flaky-test history, the coverage index (once built), and the schedules of the last few parallel runs forrstest replay. In CI, persist it between runs to keep the warm-cache speedup (CI quickstart)..pytest_cache/is pytest's, as under pytest:--lfand--ffread it.
The full file list is in Caching.
When something fails¶
Coming from pytest or pytest-xdist?
If tests fail only under parallelism on a freshly migrated suite, run
rstest migrate-check first: it
classifies each parallel-only failure (for example order dependency,
wall-clock timing, or an isolation leak, where one test leaves global
state behind that breaks a later test on the same worker) and names the
fix, so you don't triage by hand. See Migrating from pytest.
A test that isn't parallel-safe¶
The one thing that can fail after switching to rstest is a test that quietly depended on running alone. Concretely, two tests writing the same file:
import json
from pathlib import Path
# Both tests use the same hard-coded path. Serially they take turns;
# in parallel they clobber each other and one fails intermittently.
def test_writes_config():
Path("output.json").write_text('{"a": 1}')
assert json.loads(Path("output.json").read_text())["a"] == 1
def test_writes_other_config():
Path("output.json").write_text('{"b": 2}') # same file!
assert json.loads(Path("output.json").read_text())["b"] == 2
Two ways out. Best: make them independent with tmp_path, pytest's
per-test temp directory, so they never share a file:
import json
def test_writes_config(tmp_path):
p = tmp_path / "output.json" # unique dir per test
p.write_text('{"a": 1}')
assert json.loads(p.read_text())["a"] == 1
Quick fix: if you can't fix it right now, mark the offending tests
serial so rstest never runs them at the same time as anything else:
from pathlib import Path
import pytest
@pytest.mark.serial # runs alone, after the parallel tests
def test_writes_config():
Path("output.json").write_text('{"a": 1}')
...
serial is the pressure valve, not the goal: it removes the speed win for
those tests, so fix the sharing when you can. Not sure which tests are
affected? rstest migrate-check finds
and classifies them for you. See Parallel safety
for the full catalogue of sharing patterns and fixes.
Session fixtures run once per worker¶
The other common surprise: a scope="session" fixture runs once per
worker, not once per run (the same as pytest-xdist). A session fixture
that creates a fixed file collides across workers:
import sqlite3
import pytest
@pytest.fixture(scope="session")
def db():
conn = sqlite3.connect("test.db") # every worker opens the same file
yield conn
conn.close()
Give each worker its own copy. tmp_path_factory is already per worker
under rstest, so this is the simplest fix:
@pytest.fixture(scope="session")
def db(tmp_path_factory):
conn = sqlite3.connect(tmp_path_factory.mktemp("db") / "test.db")
yield conn
conn.close()
For a resource outside the temp directory (a database name, a port), key it
on the worker_id fixture: gw0, gw1, ... at -n ≥ 2, and "master"
below -n 2, where there is only one process. See
Parallel safety.
Next steps¶
- Migrating from pytest: what changes and what to check before you rely on rstest in CI.
- CI quickstart: GitHub Actions with a persisted cache.
- Parallel safety: finding and fixing tests that can't run in parallel.
- Troubleshooting: first-run errors and their fixes.