Coverage.py
Coverage.py measures test coverage for Python programs. It records which lines ran during test execution and can optionally track branch coverage, then reports which code paths were hit and which were missed.
Installation and Setup
Install it from PyPI:
Optional configuration with a .coveragerc or pyproject.toml file:
.coveragerc
[run]
branch = True
source = src
[report]
show_missing = True
omit =
tests/*
Key Features
- Supports line and branch coverage measurement with optional exclusion rules and pragmas.
- Offers selectable measurement cores through the
[run] coresetting, including thesysmoncore built onsys.monitoring, which is the default on Python 3.14 and later. - Supports multiple report formats including terminal, HTML, XML, JSON, and LCOV.
- Provides concurrency support for
multiprocessing,gevent,greenlet, andeventletthrough the[run] concurrencysetting, and can measure Python subprocesses with[run] patch = subprocess, combining the collected data afterward. - Offers configurable measurement through
.coveragercorpyproject.toml. - Supports data management commands to combine, erase, and annotate results for CI or local workflows.
Usage
Measure coverage while running your test suite:
$ coverage run -m pytest -q
Show a summary in the terminal with missing lines:
$ coverage report -m
Generate an HTML report:
$ coverage html
Export machine-readable outputs:
$ coverage xml
$ coverage json
Reset collected data and start fresh:
$ coverage erase
Related Resources
Tutorial
pytest Tutorial: Effective Python Testing
Master pytest with this hands-on tutorial. Learn fixtures, parametrize, marks, and plugins to write fast, effective Python test suites.
For additional information on related topics, take a look at the following resources:
- Testing Your Code With pytest (Course)
- Test-Driven Development With pytest (Course)
- Testing Your Code With Python's unittest (Course)
- Effective Testing with Pytest (Quiz)
By Leodanis Pozo Ramos • Updated Sept. 7, 2026