# pytest

Your pytest suite gets one thing from [Variance Authority](https://variance-authority.dev/docs/index.md) that
everything else builds on: a record of which functions of your code each test
ran. `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:

```bash
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:

```json
{ "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:

```bash
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:

```bash
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

```bash
variance covering --file pkg/calc.py --function add
```

```text
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](https://variance-authority.dev/docs/python-reference/index.md#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](https://variance-authority.dev/docs/test-stories/index.md) and visual review.

## Go further

- [The pytest reference](https://variance-authority.dev/docs/python-reference/index.md): how CPython reports the lines, and what the command writes.
- [When a left-out pytest test could still have failed](https://variance-authority.dev/docs/python-reference/index.md#when-a-left-out-test-could-still-have-failed), and what `before` closes.
- [The `select` command](https://variance-authority.dev/reference/packages/cli/index.md#select-what-your-own-runner-may-skip): every format and what stderr says.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts pytest beside every other runner.
