Skip to main content

Run pytest and report coverage only for git-affected modules.

Project description

pytest-cov-affected

CI

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

  1. Local staged/unstaged changes are checked first; if any exist, their changed .py files under src_root become the affected set.
  2. If there are no local changes, git diff against the configured base produces the list of changed .py files under src_root.
  3. Each path is mapped to its expected test file; missing tests are reported.
  4. Pytest's collection is narrowed to the matching test files only.
  5. The active coverage.py instance's include patterns are constrained to the affected sources so measurement is scoped, or a managed coverage session is started when pytest-cov is not active.
  6. At session end, the .coverage SQLite data file is filtered in place so later coverage html / coverage xml calls report only affected modules. A .coveragerc.affected sidecar is written alongside for users who prefer coverage --rcfile.

Documentation

See docs/:

Development

uv sync
uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_cov_affected-0.1.0.tar.gz (86.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_cov_affected-0.1.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

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

Hashes for pytest_cov_affected-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0711eddc938e39c2964d8b7a0ba3ac5a3a33cddfa78393e5fab4e115f558d566
MD5 68398ade8171743f22fb5a8d8acb3de2
BLAKE2b-256 0cbe1fcb13ef8eadb1d405326f0e2f5ae9c03936667be6f77283ca3e06a1cf92

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_cov_affected-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_cov_affected-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8edef81ab6dc73d40326502256eae4c7b486480561fc3eeb11299bd43f6f18f
MD5 453bd3b167ce8280534d68a2377b4439
BLAKE2b-256 dc099cf7a8205119d17b88d5156eb5c2004782dc8602daadc2df55177ba1a79b

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page