Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

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

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.

intermediate testing

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


By Leodanis Pozo Ramos • Updated Dec. 15, 2025