Python Coverage Report
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.
Right
# 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 pytestinstead ofcoverage run pytest - Specify the source directory in
.coveragerc - Run
coverage erasebefore collecting data to avoid stale data issues
Common Mistakes with coverage report
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro