Your test suite
pytest
variance python runs pytest with a recorder loaded, under CPython 3.12
or newer, and writes it. Then variance select names the test files a change
did not reach, and variance covering names the tests that ran a function.Try it
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
Declare the suite in variance.config.json at the repository root. The files
in before are ones no test reads, so a change to any of them runs every test:
{ "suites": { "unit": { "kind": "unit", "before": ["pyproject.toml", "requirements.txt", "conftest.py"] } } }
Record the whole suite once, from the directory pytest runs in. What follows
-- goes to pytest:
variance python --suite unit -- tests/ -q
It exits 0 when every test passed and 1 when one failed. Record without -k
or -m: a test the narrowed run left out has no row, so it is never left out
later.
Now change a function and hand pytest the files to leave out, through its own
--ignore, from the repository root:
pytest tests/ -q $(variance select --suite unit --since origin/main | sed 's/^/--ignore=/')
What you get
Leave out the test files a change did not reach
pytest does not leave files out itself; variance select names them.
variance select prints a skip list, never a run list: on stdout, one test
file per line, relative to the repository, the files the change did not reach.
On stderr it prints one summary, such as skipping 212 of 240 test files recorded whole: none covered a changed line; every other test file runs. An
empty list means every test file runs.
Ask which tests ran a function
variance covering --file pkg/calc.py --function add
1 named test covered function add of pkg/calc.py:
tests/test_calc.py — 1/2
tests/test_calc.py::test_add
A method or nested function is a region of its own, named by its scope, such as
Calc/twice/inner. What a function is
says what else counts.
Not yet
- A pytest plugin that leaves files out itself:
--ignore, as above, does it. - Skipping cases inside a kept file.
- Shard placement by recorded time.
- Test stories and visual review.
Go further
- The pytest reference: how CPython reports the lines, and what the command writes.
- When a left-out pytest test could still have failed, and what
beforecloses. - The
selectcommand: every format and what stderr says. - Supported frameworks puts pytest beside every other runner.