with one click
pytest-coverage
// Code coverage measurement for pytest via pytest-cov — terminal/HTML/XML reports, xdist support, coverage contexts
// Code coverage measurement for pytest via pytest-cov — terminal/HTML/XML reports, xdist support, coverage contexts
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | pytest-coverage |
| description | Code coverage measurement for pytest via pytest-cov — terminal/HTML/XML reports, xdist support, coverage contexts |
| tech_stack | ["backend"] |
| language | python |
| capability | ["unit-testing","ci-cd"] |
| version | pytest-cov 7.x |
| collected_at | "2025-01-01T00:00:00.000Z" |
Source: https://pytest-cov.readthedocs.io/en/latest/, https://github.com/pytest-dev/pytest-cov
Measure code coverage during pytest runs using the coverage package. Provides automatic data-file management, terminal/HTML/XML/JSON reporting, branch coverage, per-test contexts, full xdist (parallel) support, and failure thresholds — all through pytest CLI flags or standard coverage config files.
--cov-fail-under=80)--cov-context=testpip install pytest-cov
# Basic: measure coverage on myproj, show terminal report
pytest --cov=myproj tests/
# With missing lines, HTML report, and branch coverage
pytest --cov=myproj --cov-report=term-missing --cov-report=html --cov-branch
# Fail if under 80%
pytest --cov=myproj --cov-fail-under=80
Output:
-------------------- coverage: ... ---------------------
Name Stmts Miss Cover
----------------------------------------
myproj/__init__ 2 0 100%
myproj/myproj 257 13 94%
myproj/feature4286 94 7 92%
----------------------------------------
TOTAL 353 20 94%
| Flag | Purpose |
|---|---|
--cov=PATH | Measure coverage for this package/module (required) |
--cov-report=FORMAT | term, term-missing, html, xml, json, lcov, annotate (repeatable) |
--cov-branch | Enable branch coverage |
--cov-fail-under=N | Exit with error if coverage < N% |
--cov-append | Append to existing .coverage instead of erasing |
--cov-context=test | Per-test coverage context (includes parametrize ids) |
--cov-config=PATH | Path to .coveragerc/pyproject.toml |
--no-cov | Disable coverage (override config) |
--no-cov-on-fail | Skip writing .coverage data if any tests failed |
All standard coverage options work in .coveragerc, pyproject.toml, setup.cfg, or tox.ini:
# pyproject.toml
[tool.coverage.run]
source = ["myproj"]
branch = true
omit = ["*/tests/*", "*/migrations/*"]
concurrency = ["multiprocessing"] # for subprocess coverage
[tool.coverage.report]
fail_under = 80
show_missing = true
# pytest.ini
[pytest]
addopts = --cov=myproj --cov-report=term-missing --cov-report=html
@pytest.mark.no_cover — exclude this test from coverage measurement@pytest.mark.cov(...) — per-test coverage configurationpytest --cov=myproj -n 4 # each worker writes own .coverage, auto-combined
pytest --cov=myproj --cov-append -n 4 # workers append to shared data
All workers must have pytest-cov installed. Coverage data is automatically combined at the end.
The .pth-file approach was removed in v7. Use coverage's native concurrency support:
[run]
concurrency = multiprocessing
.pth-file-based subprocess coverage was removed in v7. Migrate to concurrency = multiprocessing in coverage config.coverage run vs pytest --cov: coverage run -m pytest adds CWD to sys.path; pytest --cov does not. This can cause import resolution differences.--no-cov in local dev, enable only in CI.pytest --cov in the same directory without xdist. Use COVERAGE_FILE env var or separate output dirs if needed..pth files: Old pytest-cov.pth or init_cov_core.pth may remain in site-packages after uninstall — remove manually if coverage behaves unexpectedly..coverage is erased at start of each run (unless --cov-append) and left after testing for post-run tooling.--cov-fail-under in CI to enforce minimums.pytest-xdist alongside pytest-cov for parallel coverage; results auto-combine.--cov-context=test tags each parametrized variant separately, enabling per-case coverage analysis.--cov-report=xml) for services like Codecov/Coveralls, HTML (--cov-report=html) for artifact archives.