pytest-cov "No data was collected" / module-not-measured in CI
coverage.py recorded no data: the --cov target did not match the imported package, the code was already imported before coverage started, or tests ran against an installed copy instead of the source tree.
What this error means
The run warns "CoverageWarning: No data was collected. (no-data-collected)" or "Module X was never imported," and total coverage shows 0% or the report is empty.
WARNING: Failed to generate report: No data to report.
/.../coverage/control.py: CoverageWarning: No data was collected. (no-data-collected)Common causes
The --cov path does not match the imported package
Passing --cov=mypackage while tests import an installed copy from site-packages means coverage watches a path no code runs in.
The package was imported before coverage started
Importing the package at collection time, or via a conftest, can load it before measurement begins, so lines are not counted.
How to fix it
Point --cov at the actual source and install editable
- Install the project editable so tests import the source tree, not a built copy.
- Set
--covto the import name orsrcpath that tests actually load. - Re-run and confirm coverage is non-zero.
pip install -e .
pytest --cov=src --cov-report=termMeasure with the source layout in config
Declare the source in .coveragerc so coverage maps the installed package back to your source files.
# .coveragerc
[run]
source = src
relative_files = trueHow to prevent it
- Install the project editable in CI so imports hit the source tree.
- Match
--cov/sourceto the package tests import. - Avoid importing the package before coverage starts.