Skip to content

Measuring Test Coverage with Coverage.py

Code coverage is a metric that indicates how much of an application’s source code is executed when the test suite runs, typically expressed as a percentage of the codebase exercised by the tests. pytest is commonly used together with Coverage.py tool to measure code coverage. While Coverage.py can be used on its own, pytest users typically rely on the pytest-cov plugin, which provides seamless integration.

After installing the plugin, coverage measurement becomes part of the normal pytest workflow:

shell
pytest --cov=my_package

This command runs all tests and reports how much of my_package was covered.

You can also generate more detailed reports, such as an HTML report:

shell
pytest --cov=my_package --cov-report=html