Sometimes, you want to ignore parts of your code when checking test coverage. This helps focus on important code and avoid false alarms.
Excluding code from coverage in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
# Use comment # pragma: no cover after the line you want to exclude some_code() # pragma: no cover
The comment # pragma: no cover tells coverage tools to ignore that line.
This works well with pytest and coverage.py tools.
Examples
PyTest
def example(): print("This runs") # pragma: no cover
PyTest
if __name__ == '__main__': # pragma: no cover main()
PyTest
def debug(): # This function is for debugging only print("Debug info") # pragma: no cover
Sample Program
This example shows a simple function add tested by test_add. The debug_log function is excluded from coverage using # pragma: no cover.
PyTest
def add(a, b): return a + b def debug_log(): print("Debug info") # pragma: no cover def test_add(): assert add(2, 3) == 5 # Run tests with coverage: pytest --cov=. # The debug_log function line is excluded from coverage report.
Important Notes
Use # pragma: no cover only on lines you really want to exclude.
Coverage tools ignore these lines when calculating coverage percentage.
Remember to run coverage with pytest using --cov option.
Summary
Use # pragma: no cover comment to exclude code lines from coverage.
This helps keep coverage reports focused and accurate.
Works well with pytest and coverage.py tools.
Practice
1. What is the purpose of adding
# pragma: no cover in your Python test code when using pytest and coverage.py?easy
Solution
Step 1: Understand coverage exclusion
The comment# pragma: no covertells coverage.py to ignore that line when measuring test coverage.Step 2: Purpose in test reports
This helps keep coverage reports focused on meaningful code, excluding lines like debug prints or platform-specific code.Final Answer:
To exclude specific lines from the coverage report -> Option AQuick Check:
Exclude lines = D [OK]
Hint: Use '# pragma: no cover' to skip lines in coverage report [OK]
Common Mistakes:
- Thinking it marks lines to always test
- Confusing it with pytest debug flags
- Assuming it generates tests automatically
2. Which of the following is the correct way to exclude a single line from coverage in a Python file using pytest and coverage.py?
easy
Solution
Step 1: Identify correct pragma syntax
The correct syntax to exclude a line is# pragma: no coverexactly as written.Step 2: Match options to syntax
Only print('Debug info') # pragma: no cover uses the exact correct comment syntax recognized by coverage.py.Final Answer:
print('Debug info') # pragma: no cover -> Option CQuick Check:
Exact pragma syntax = A [OK]
Hint: Remember exact comment: '# pragma: no cover' [OK]
Common Mistakes:
- Swapping words in the pragma comment
- Using incorrect comment keywords
- Missing 'pragma:' keyword
3. Consider this Python code snippet tested with pytest and coverage.py:
def func(x):
if x > 0:
return x
else:
return -x # pragma: no cover
What will coverage.py report about the line with return -x if it is never executed?medium
Solution
Step 1: Understand pragma effect on coverage
The comment# pragma: no covertells coverage.py to ignore that line regardless of execution.Step 2: Effect on coverage report
Since the line is ignored, not executing it does not reduce coverage percentage.Final Answer:
The line will be ignored and not affect coverage percentage -> Option BQuick Check:
Pragma ignores line in coverage = B [OK]
Hint: Lines with '# pragma: no cover' don't lower coverage [OK]
Common Mistakes:
- Assuming ignored lines count as uncovered
- Expecting test failure due to coverage
- Thinking pragma causes syntax errors
4. You wrote this code:
def example():
print('Start') # pragma no cover
print('End')But coverage.py still counts the first print line as uncovered. What is the likely problem?medium
Solution
Step 1: Check pragma syntax
The correct syntax requires a colon:# pragma: no cover. Missing colon causes coverage.py to ignore the comment.Step 2: Effect of incorrect syntax
Without the colon, coverage.py treats the line normally and counts it as uncovered if not executed.Final Answer:
The pragma comment is missing the colon after 'pragma:' -> Option DQuick Check:
Colon required in pragma comment = A [OK]
Hint: Always include colon: '# pragma: no cover' [OK]
Common Mistakes:
- Omitting colon after 'pragma:'
- Placing comment on wrong line
- Thinking coverage can't exclude print statements
5. You want to exclude a block of code from coverage in a pytest project, but
# pragma: no cover only works line-by-line. Which approach correctly excludes multiple lines without affecting other code?hard
Solution
Step 1: Understand line-by-line exclusion
The# pragma: no covercomment excludes coverage only for the line it is on, so each line must have it to be excluded.Step 2: Evaluate other options
Wrapping in a function or usingif False:changes code behavior or testability; partial comments on first and last lines do not exclude intermediate lines.Final Answer:
Add '# pragma: no cover' comment to each line in the block -> Option AQuick Check:
Exclude multiple lines by commenting each line = C [OK]
Hint: Comment each line with '# pragma: no cover' to exclude block [OK]
Common Mistakes:
- Assuming one comment excludes multiple lines
- Using 'if False:' which affects runtime
- Trying to exclude with decorators without support
