Run pytest and report coverage only for git-affected modules.
Project description
pytest-cov-affected
A pytest plugin that runs only the tests for source modules you changed and reports coverage exclusively for those modules.
Why
Running the full test suite and full coverage report on every change is slow
and the resulting coverage number is diluted by code you didn't touch. With
--cov-affected, pytest collects only tests/<rel>/test_<module>.py for the
src/<pkg>/<rel>/<module>.py files in your local staged/unstaged changes. If
the tree is clean, it falls back to comparing against the merge-base with
main. Any subsequent coverage html / coverage xml reports contain rows
only for those modules.
--cov is optional and does not change the affected set.
Install
uv add --dev pytest-cov-affected
# or
pip install pytest-cov-affected
Usage
# run only tests for modules changed in local edits, or since merge-base with main
pytest --cov-affected
# show the coverage table without needing `--cov`
pytest --cov-affected --cov-report term-missing
# optional: also enable pytest-cov terminal reporting
pytest --cov-affected --cov
# pick a different diff base
pytest --cov-affected --cov-affected-base=origin/release
# include new untracked .py files under src/
pytest --cov-affected --cov-affected-include-untracked
# follow up with coverage reports — these will only contain affected modules
coverage html
coverage xml
Convention
The plugin assumes the standard src/ layout:
src/<package>/path/to/module.py ⇒ tests/path/to/test_module.py
Configurable via:
--cov-affected-src-root(default:src)--cov-affected-tests-root(default:tests)
Source files without a matching test file produce a UserWarning and are
counted in the summary line — they don't fail the run.
How it works
- Local staged/unstaged changes are checked first; if any exist, their changed
.pyfiles undersrc_rootbecome the affected set. - If there are no local changes,
git diffagainst the configured base produces the list of changed.pyfiles undersrc_root. - Each path is mapped to its expected test file; missing tests are reported.
- Pytest's collection is narrowed to the matching test files only.
- The active
coverage.pyinstance'sincludepatterns are constrained to the affected sources so measurement is scoped, or a managed coverage session is started whenpytest-covis not active. - At session end, the
.coverageSQLite data file is filtered in place so latercoverage html/coverage xmlcalls report only affected modules. A.coveragerc.affectedsidecar is written alongside for users who prefercoverage --rcfile.
Documentation
See docs/:
Development
uv sync
uv run pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_cov_affected-0.1.0.tar.gz.
File metadata
- Download URL: pytest_cov_affected-0.1.0.tar.gz
- Upload date:
- Size: 86.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0711eddc938e39c2964d8b7a0ba3ac5a3a33cddfa78393e5fab4e115f558d566
|
|
| MD5 |
68398ade8171743f22fb5a8d8acb3de2
|
|
| BLAKE2b-256 |
0cbe1fcb13ef8eadb1d405326f0e2f5ae9c03936667be6f77283ca3e06a1cf92
|
Provenance
The following attestation bundles were made for pytest_cov_affected-0.1.0.tar.gz:
Publisher:
ci.yml on AI-Colleagues/pytest-cov-affected
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_cov_affected-0.1.0.tar.gz -
Subject digest:
0711eddc938e39c2964d8b7a0ba3ac5a3a33cddfa78393e5fab4e115f558d566 - Sigstore transparency entry: 1435882489
- Sigstore integration time:
-
Permalink:
AI-Colleagues/pytest-cov-affected@d247440f3e31cb13a5c8ffc637831598ee7f2074 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AI-Colleagues
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@d247440f3e31cb13a5c8ffc637831598ee7f2074 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_cov_affected-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_cov_affected-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8edef81ab6dc73d40326502256eae4c7b486480561fc3eeb11299bd43f6f18f
|
|
| MD5 |
453bd3b167ce8280534d68a2377b4439
|
|
| BLAKE2b-256 |
dc099cf7a8205119d17b88d5156eb5c2004782dc8602daadc2df55177ba1a79b
|
Provenance
The following attestation bundles were made for pytest_cov_affected-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on AI-Colleagues/pytest-cov-affected
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_cov_affected-0.1.0-py3-none-any.whl -
Subject digest:
f8edef81ab6dc73d40326502256eae4c7b486480561fc3eeb11299bd43f6f18f - Sigstore transparency entry: 1435882500
- Sigstore integration time:
-
Permalink:
AI-Colleagues/pytest-cov-affected@d247440f3e31cb13a5c8ffc637831598ee7f2074 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AI-Colleagues
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@d247440f3e31cb13a5c8ffc637831598ee7f2074 -
Trigger Event:
push
-
Statement type: