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:

Windows PowerShell
PS> py -m pip install --user coverage
PS> coverage --version
Shell
$ python -m pip install coverage

Optional configuration with a .coveragerc or pyproject.toml file:

Config 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 .coveragerc or pyproject.toml.
  • Supports data management commands to combine, erase, and annotate results for CI or local workflows.

Usage

Measure coverage while running your test suite:

Shell
$ coverage run -m pytest -q

Show a summary in the terminal with missing lines:

Shell
$ coverage report -m

Generate an HTML report:

Shell
$ coverage html

Export machine-readable outputs:

Shell
$ coverage xml
$ coverage json

Reset collected data and start fresh:

Shell
$ coverage erase

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.

intermediate testing

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Dec. 15, 2025