What if your coverage report lied to you about how well your code is tested?
Why Excluding code from coverage in PyTest? - Purpose & Use Cases
Imagine you have a big project with many lines of code. Some parts are just for debugging or only run on special machines. You want to check how much of your code is tested, but these special parts make the numbers look bad.
Manually ignoring these parts means you have to remember which lines to skip every time you check coverage. It's slow and easy to forget, so your coverage reports become confusing and less useful.
By excluding code from coverage automatically, you tell the tool exactly which parts to ignore. This keeps your reports clean and focused on the important code you want tested.
# Run coverage and try to guess which lines to ignore later coverage run -m pytest coverage report # Manually subtract lines from report
# Mark code to exclude with comments # pragma: no cover coverage run -m pytest coverage report # Excluded lines are automatically ignored
You get clear, accurate coverage reports that truly reflect your tested code, helping you focus on improving quality.
A developer adds debug print statements to troubleshoot a bug. By excluding these lines from coverage, the report doesn't count them as untested, so the coverage score stays meaningful.
Manual coverage checks can be confusing and error-prone.
Excluding code automatically keeps reports accurate.
This helps focus testing efforts on real, important code.