VARIANCE AUTHORITY
Browsepytest reference

Your test suite

pytest reference

This page is the reference for recording a pytest suite with variance python: how CPython reports the lines, what the command runs and writes, what counts as a function, and when a test file a selection leaves out could still have failed. pytest sets the suite up in a few lines.

CPython reports the lines itself, through sys.monitoring (PEP 669), so the source is never rewritten and no coverage package is installed. A line costs one event per test: the recorder disarms it once seen and arms it again at the next test. It needs CPython 3.12 or newer and pytest.

Record which tests ran each line

The CLI is a Node package, so you need Node 22.15 or newer. Install it at the repository root:

npm install --save-dev @variance-authority/cli

Then, from the directory pytest runs in:

variance python --suite unit

unit is a suite you name under suites in variance.config.json at the repository root, as { "suites": { "unit": { "kind": "unit" } } }. What follows -- goes to pytest: the paths, markers and options to run.

variance python --suite unit -- tests/ -q

The command:

  • runs pytest once, with the recorder loaded as a plugin, so tests share one process as pytest runs them;
  • writes the suite's execution record, as every other runner does: one row per test file, one case per pytest node id.

It exits 0 when every test passed and 1 when one failed.

What a function is

A function, method or nested function is a region of its own, named by its scope, such as Calc/twice/inner. A class adds a scope and is not a region; a lambda and a decorator belong to the function they are written in. The code a module runs as it is imported belongs to the module's root, and a test that ran any function of a file is charged the root too: a module imported at a test file's top level runs once, while pytest collects the test files, so an edit to it selects every test that ran the file. A test that only read a value the file's top level set is not charged.

A file pytest imports from outside the repository root, or from site-packages, is not recorded.

When a left-out test could still have failed

All tests share one pytest process, so work pytest does once for many tests is recorded under one of them. Each case below can leave out a test file that would have failed. The whole run on main still runs it, and a suite's before closes each one until the recorder does: a change to a file it names runs every test.

  • A shared fixture or a cached function. A fixture with session or package scope, and a function cached across test files, such as one under functools.cache, run under the first test that needs them, and only that test is charged. Name the file that holds one in before.

  • A value read at import. A test that ran none of a module's functions and only read a value its top level set, such as a constant, is not charged for that module. Name the module in before.

  • A dependency, a pytest setting or a hook. No test reads pyproject.toml, requirements.txt, a lockfile, pytest.ini, setup.cfg or tox.ini, and a conftest.py's top level and hooks run outside every test, so a change to one keeps no test in the run. Name them in before:

    { "suites": { "unit": { "kind": "unit", "before": ["pyproject.toml", "requirements.txt", "conftest.py"] } } }
    
  • A narrowed recording. A run narrowed with -k or -m records only the tests it ran, yet takes their files as finished, so the tests it left out stop being selected. Record the whole suite.