# Python (pytest)

> How trimtest selects pytest tests, records coverage with coverage.py, and what it needs.

| | |
|---|---|
| Runner | pytest |
| Static graph | tree-sitter imports, the package `__init__` chain, `conftest.py` scoping |
| Recorded tier | yes, with `coverage` installed; function-level |
| Runner prefix | `[runner] python = "uv run"` (detected from lockfiles otherwise) |

## Static graph

Imports are parsed with tree-sitter. A package's `__init__.py` chain counts as a dependency, and a changed
`conftest.py` selects the tests it scopes. Use `import_roots = [dirs]` in [`trimtest.toml`](/reference/config/) for
extra import roots (a `src/` layout, for example). A pyproject, `setup.py` or `pytest.ini` marks a project;
workspace members (uv, PDM) are part of their parent.

## Recording

```bash
pip install coverage
trimtest record --coverage
```

Each test file runs on its own under coverage.py, with `-p no:xdist` so workers do not hide executions. A
project's `.coveragerc` is honoured (only its `concurrency` setting is kept, so the recording stays measurable).
Selection is then [function-level](/concepts/function-level/).

Coverage runs in the same environment as the tests: with a `uv.lock` or `poetry.lock` the command becomes
`uv run python -m coverage ...` / `poetry run python -m coverage ...` — or, when the project keeps its
environment in `.venv` (or points `UV_PROJECT_ENVIRONMENT` at one), that interpreter directly — so `coverage` has to be installed in
that environment rather than on the system interpreter (`uv pip install coverage`, `poetry add --group dev
coverage`). Otherwise the recording comes out empty and trimtest falls back to selecting every test.

## Things to know

- Tests that start other processes (`subprocess`, `os.system`, `asyncio.create_subprocess_*`,
  `ProcessPoolExecutor`, `os.fork`, `pty`) cannot be measured through their children, so they run on any change.
- Decorator and default-parameter changes count as a change to the function they belong to.
- `getattr(mod, "NAME")` and other access by string is not seen by name matching.
