Skip to content

Python Coverage Report

DodaTech 1 min read

In this tutorial, you'll learn about Python Coverage Report Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

Coverage.py warning: No data was collected. (no-data-collected)
Running `coverage run` did not collect any coverage data.

The test runner did not execute under coverage monitoring.

Wrong

coverage run pytest tests/

Output: No data was collected. if the command does not match how pytest runs, or if tests import modules in a way that bypasses the trace function.

# Run tests with coverage
coverage run -m pytest tests/

# Generate report
coverage report -m

# Generate HTML report
coverage html

Use a .coveragerc file:

[run]
source = my_package
omit = */tests/*,*/migrations/*

[report]
show_missing = true
skip_empty = true

Expected output: coverage report showing percentage covered per module.

Prevention

  • Use coverage run -m pytest instead of coverage run pytest
  • Specify the source directory in .coveragerc
  • Run coverage erase before collecting data to avoid stale data issues

Common Mistakes with coverage report

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between coverage report and coverage html?

coverage report prints a text table to the terminal. coverage html generates an HTML report in htmlcov/ with color-coded source code showing which lines are covered.

How do I combine coverage from multiple test runs?

Run coverage run for each test suite, then coverage combine to merge the .coverage data files. Use coverage report to generate the combined report.

How do I exclude lines from coverage?

Add # pragma: no cover at the end of a line or # pragma: no branch for branch coverage. Use .coveragerc exclude_lines to match patterns.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro