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.
- Supports multiple report formats including terminal, HTML, XML, and JSON.
- Provides concurrency support with data collected from subprocesses and combined later.
- 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
Effective Python Testing With pytest
In this tutorial, you'll learn how to take your testing to the next level with pytest. You'll cover intermediate and advanced pytest features such as fixtures, marks, parameters, and plugins. With pytest, you can make your test suites fast, effective, and less painful to maintain.
For additional information on related topics, take a look at the following resources:
- Testing Your Code With pytest (Course)
- Effective Testing with Pytest (Quiz)
By Leodanis Pozo Ramos • Updated Dec. 15, 2025