Skip to main content

Code coverage — measuring test effectiveness

Mocks from article 151 let you steer tests, but they do not reveal unvisited lines. Coverage tools highlight execution counts so you can prioritize tests where risk concentrates—not chase 100% for vanity.

pip install coverage pytest-cov

📚 Prerequisites

  • Working pytest suite (even a small one).

🎯 What you'll learn

  • Run pytest --cov locally and read terminal summaries.
  • Interpret coverage reports without gaming the metric.

Quick pytest integration

pytest --cov=myproject --cov-report=term-missing

term-missing annotates which lines lacked hits—ideal for tightening suites during code review.

HTML reports help explore large projects:

pytest --cov=myproject --cov-report=html

Interpreting numbers responsibly

  • High coverage with weak assertions still ships bugs—coverage only knows a line ran, not that outputs were validated.
  • Low coverage in glue code might be acceptable if orchestration is smoke-tested elsewhere.
  • Use coverage diffs in CI to ensure new modules do not land at 0%.

💡 Key takeaways

  • Treat coverage as a map, not a badge; pair with risk analysis and meaningful assertions.

➡️ Next steps

Package your project for others in Creating a setup.py file.