pytest-cover
pytest-cover is a pytest plugin for measuring code coverage, which was forked from pytest-cov. The project was officially merged back into pytest-cov in January 2019, and its GitHub repository explicitly states to use pytest-cov instead. This library (version 3.0.0) is effectively abandoned and users are strongly advised to use the actively maintained `pytest-cov` for coverage reporting with pytest.
Warnings
- breaking The `pytest-cover` project is officially abandoned and has been merged back into `pytest-cov` since January 2019. Further development, bug fixes, or compatibility updates for `pytest-cover` are not expected. Using this library in new projects or maintaining it in existing ones is strongly discouraged.
- gotcha Older versions of `pytest-cover` (and `pytest-cov` before version 7) relied on `.pth` files for subprocess coverage measurement, which could lead to issues, especially with `easy_install` or specific uninstallation scenarios leaving stray files.
- breaking Given its abandonment in 2019, `pytest-cover` (version 3.0.0) is unlikely to be compatible with recent Python versions (e.g., Python 3.9+) or the latest versions of `pytest` itself, which frequently introduce breaking changes or drop support for older Python versions. For example, `pytest` 8 dropped support for Python 3.7.
- gotcha The old `pytest-cover` README warned that for distributed testing, workers (slaves) must have `pytest-cover` installed, and environment variables for subprocess measurement must propagate. Similar considerations apply to `pytest-cov` with `pytest-xdist`.
Install
-
pip install pytest-cover -
pip install pytest-cov
Quickstart
# This quickstart is for the recommended alternative: pytest-cov
# Install required packages: pip install pytest pytest-cov
# my_module.py
def my_function(x):
if x > 0:
return x * 2
else:
return 0
# test_my_module.py
from my_module import my_function
def test_positive_input():
assert my_function(5) == 10
def test_zero_input():
assert my_function(0) == 0
def test_negative_input():
assert my_function(-3) == 0
# To run tests and generate a coverage report (terminal output):
# pytest --cov=my_module
# To generate an HTML report:
# pytest --cov=my_module --cov-report=html
# Example of running the command (execute in your terminal where files are saved):
# import os
# os.system('pytest --cov=my_module')
# os.system('pytest --cov=my_module --cov-report=html')