Skip to content

Markers

The pytest markers rstest gives special meaning to, and how each behaves at different worker counts.

@pytest.mark.serial

import pytest


@pytest.mark.serial
def test_binds_port_8080(): ...

Excludes the test from the parallel phase. Serial tests run exclusively: on a single designated worker, only after every other worker's session has fully finished (fixtures torn down, ports and databases released), in collection order.

The marker is registered by rstest automatically: no markers ini entry is needed, and --strict-markers does not complain under rstest.

Under plain pytest (no rstest) the marker is unregistered. It changes no behavior, but pytest warns PytestUnknownMarkWarning, so a run with --strict-markers (or -W error) fails at collection. The same applies to flaky and timeout when pytest-rerunfailures / pytest-timeout are not installed. To keep such a run working, add the names to your markers ini entry. See What ties you to rstest.

Semantics details in Scheduling; when to use it in Parallel safety.

@pytest.mark.isolated

import pytest


@pytest.mark.isolated
def test_patches_a_global_and_never_restores_it(): ...

Runs the test in a forked child of its worker (Unix). Whatever it changes in the process (module globals, os.environ, sys.modules, a monkeypatch it never undid, a thread or file descriptor it left open) dies with the child, so the tests after it see the process as it was. Use it to contain a polluter that rstest bisect or --fail-on-leak found, while it stays in the parallel run, until it is fixed.

  • Works at every worker count, -n 0 included, and with --collect lazy.
  • Reports come back as usual: pass, fail, skip and teardown errors. A child that dies before reporting (a segfault, os._exit) is a failed test (isolated test crashed: its child process ...), not a crashed worker.
  • Fixtures. The worker keeps every fixture it had already set up, and the child tears down only what it added. So nothing is torn down twice, and nothing the child created is left behind. The cost: a module, class or session fixture an isolated test is the first to use is set up in the child and set up again by the next test that needs it.
  • A fork costs a few milliseconds; mark the tests that need it, not whole modules.
  • Windows has no fork(): the test runs in its worker as usual, with a one-time warning.
  • What the child records itself is not carried back: warnings it raises and --doctor CPU time.

Registered by rstest, like serial; plain pytest warns about it as an unknown marker (see serial above).

@pytest.mark.flaky

@pytest.mark.flaky(reruns=3)
def test_talks_to_flaky_service(): ...

Per-test rerun budget: the mark overrides a global --reruns for that test. A pass-after-retry reports as flaky exactly like global reruns. The plugin itself is neutralized inside rstest pool workers to prevent double reruns (at -n 0/1 without a global --reruns there is no pool, so it stays active; see below). Registered automatically.

The marker name matches pytest-rerunfailures, and rstest reads the budget the same way: the reruns= keyword, else the first positional argument (flaky(3)), else 1. condition= is honored too: a false condition (a bool, or a string evaluated like the plugin does) means no reruns. A string condition that fails to evaluate keeps the reruns. only_rerun and rerun_except (a regex or an exception class, or a list) filter by error as the plugin does: each exception in the raised one's chain is matched as Type: message. reruns_delay waits that many seconds before a rerun (in a pool, after every failed attempt that may be retried, the last one included). The global --only-rerun applies on top.

The mark works with or without a global --reruns, at any worker count:

  • At -n ≥ 2 (and in the one-worker rerun pool that --reruns starts at -n 0/1, see --reruns) the orchestrator retries the test, possibly on another worker.
  • At -n 0/1 without --reruns the run stays the byte-exact single session, and the session retries a marked test in place, the way pytest-rerunfailures does: a failed attempt shows as R (RERUN with -v) and counts as N rerun in pytest's summary line, and a test that then passes is reported flaky. An installed pytest-rerunfailures handles the mark itself there instead, as under plain pytest. Tests without the mark run exactly as before.

A failed attempt that a rerun may still rescue never counts toward -x / --maxfail; only a test that fails its last attempt does.

@pytest.mark.xdist_group

@pytest.mark.xdist_group("dbpool")
def test_uses_shared_pool(): ...

Under --dist loadgroup, all tests sharing a group name run on the same worker, across files. pytest-xdist-compatible.

rstest registers the marker automatically, so --strict-markers never complains about it under rstest, even when pytest-xdist is not installed. Portability caveat: under plain pytest, xdist_group is xdist's own marker, registered only when pytest-xdist is installed; a plain-pytest run with --strict-markers and no xdist installed will reject it. Migrating off xdist, you keep the marker either way: rstest honors it, and plain pytest treats it as inert (a no-op) as long as --strict-markers isn't forcing the issue.

@pytest.mark.timeout

@pytest.mark.timeout(5)
def test_slow_path(): ...

Per-test deadline in seconds, overriding the global --timeout. The test is interrupted in-process at the deadline and fails with a traceback at the stuck line. pytest-timeout-compatible marker name; no plugin needed.

timeout(0) or a negative value disables the timeout for that test, even when a global --timeout is set.

A positive marker value also sizes that test's hang watchdog.

The marker arms rstest's own timer at every worker count, including -n 0, even when you pass no --timeout and even with pytest-timeout installed (-p no:timeout does not turn it off). If pytest-timeout is also installed, both honor the marker, so uninstall it rather than run two timers.

A note on @pytest.mark.parametrize IDs

Not a marker rstest owns, but the one that most often blocks parallelism: parametrize ids must be stable across collections. An id built from a memory address, a uuid or a timestamp can stop a parallel run with workers collected different test sets; give such a parametrize an explicit stable ids=. Causes, fixes and how rstest migrate-check finds them: Unstable parametrize ids.