pytest --cov: Coverage Command Reference for CI
Measure and enforce test coverage as part of the run.
The pytest-cov plugin adds --cov, integrating coverage.py with pytest. CI uses it to report coverage and to fail the build when it drops below a threshold.
Common flags / usage
- --cov=PKG -- measure coverage for a package or path
- --cov-report=term-missing -- show uncovered lines in the log
- --cov-report=xml -- write coverage.xml for upload tools
- --cov-fail-under=N -- fail if total coverage is below N percent
- requires: pip install pytest-cov
Example
shell
- run: pip install pytest pytest-cov
- run: |
python -m pytest \
--cov=myapp --cov-report=xml \
--cov-fail-under=85 -qIn CI
Combining --cov with xdist (-n auto) works, but make sure subprocess code is measured (coverage concurrency settings) if you spawn processes. Emit --cov-report=xml for upload to a coverage service, and gate merges with --cov-fail-under.
Key takeaways
- pytest --cov measures coverage during the test run.
- --cov-fail-under=N turns coverage into a hard CI gate.
- Emit --cov-report=xml for coverage upload integrations.
Related guides
pytest: Run Tests Command Reference for CIReference for pytest in CI: running the suite, useful flags, why python -m pytest fixes import errors, and a…
pytest -n (xdist): Parallel Tests Command ReferenceReference for pytest -n from pytest-xdist in CI: running tests in parallel across CPUs with -n auto, distribu…
pytest -m: Select Tests by Marker Command ReferenceReference for pytest -m in CI: selecting tests by registered markers, deselecting slow tests, registering mar…