0
0
PyTesttesting~20 mins

Excluding code from coverage in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coverage Exclusion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of coverage report with excluded code

Given the following Python code with coverage exclusion comments, what will be the total coverage percentage reported by pytest-cov?

PyTest
def add(a, b):
    return a + b

# pragma: no cover
def unused_function():
    print("This function is not covered")

result = add(2, 3)
print(result)
A50%
B100%
C0%
D75%
Attempts:
2 left
💡 Hint

Code marked with # pragma: no cover is excluded from coverage.

assertion
intermediate
1:30remaining
Correct assertion for coverage exclusion

Which assertion correctly verifies that a function marked with # pragma: no cover is excluded from coverage reports?

Aassert coverage_report.total_coverage < 50
Bassert 'unused_function' in coverage_report.executed_functions
Cassert 'unused_function' not in coverage_report.executed_functions
Dassert coverage_report.total_coverage == 0
Attempts:
2 left
💡 Hint

Functions excluded from coverage should not appear in executed functions list.

🔧 Debug
advanced
2:30remaining
Why is coverage including excluded code?

Given this code snippet, coverage reports include helper_function even though it has # pragma: no cover. What is the most likely reason?

def main():
    helper_function()

# pragma: no cover
def helper_function():
    print("Helper")

main()
AThe comment is misplaced; it should be above the function
BCoverage tools ignore <code># pragma: no cover</code> inside functions
CThe <code># pragma: no cover</code> comment must be on the same line as the function definition
DThe function is called, so it cannot be excluded
Attempts:
2 left
💡 Hint

Placement of exclusion comments matters for coverage tools.

framework
advanced
2:00remaining
Configuring pytest-cov to exclude files

Which pytest-cov configuration snippet correctly excludes all files in the tests/helpers/ directory from coverage reports?

A
[coverage:report]
omit = tests/helpers/*
B
[coverage:run]
exclude = tests/helpers/*
C
[run]
exclude = tests/helpers/*
D
[run]
omit = tests/helpers/*
Attempts:
2 left
💡 Hint

Check pytest-cov documentation for the correct section and key to omit files.

🧠 Conceptual
expert
1:30remaining
Impact of excluding code on test coverage metrics

Which statement best describes the impact of excluding code with # pragma: no cover on overall test coverage metrics?

AExcluding code increases coverage percentage by removing untested code from calculation
BExcluding code decreases coverage percentage because less code is counted
CExcluding code has no effect on coverage percentage
DExcluding code causes coverage tools to report errors
Attempts:
2 left
💡 Hint

Think about how coverage percentage is calculated.