Skip to content
Latchkey

pytest-cov Reports 0% / "module-not-imported" - No Source Measured

pytest-cov ran but measured no real code, reporting 0% or warning that modules were never imported. The --cov target points at a path that is not the imported package, so coverage watches files that never load.

What this error means

Coverage prints 0% for your package, or CoverageWarning: Module X was never imported / No data was collected. Tests pass, but the report is empty because the measured path and the imported path differ (often a src-layout or installed-package mismatch).

pytest output
CoverageWarning: Module myapp was never imported. (module-not-imported)
CoverageWarning: No data was collected. (no-data-collected)
TOTAL      0      0   0%

Common causes

--cov points at the wrong path

Passing --cov=src or a directory that is not the importable package name means coverage instruments files that tests never import, so nothing is recorded.

Installed package measured instead of source (or vice versa)

With a src-layout and an installed/editable package, tests import from site-packages while --cov watches the source tree (or the reverse), so the imported module is not the measured one.

How to fix it

Point --cov at the import package name

Measure by the importable package name so coverage tracks the modules tests actually load.

Terminal
pytest --cov=myapp --cov-report=term-missing
# not --cov=src (a directory) unless that is the import name

Configure source and path mapping for src-layout

Tell coverage the source root and map installed paths back to source so measurement and imports line up.

pyproject.toml
[tool.coverage.run]
source = ["myapp"]

[tool.coverage.paths]
source = ["src/myapp", "*/site-packages/myapp"]

How to prevent it

  • Use the import package name for --cov, not a directory.
  • Configure [tool.coverage.paths] for src-layout/installed packages.
  • Fail the build on coverage warnings to catch a zero-measurement run early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →