0
0
PyTesttesting~20 mins

Why coverage measures test completeness in PyTest - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coverage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is code coverage important in testing?

Code coverage helps us understand how much of our code is tested. Why is this important?

AIt measures how fast the tests run.
BIt guarantees that all bugs are fixed in the code.
CIt replaces the need for writing test cases.
DIt shows which parts of the code are executed by tests, helping find untested areas.
Attempts:
2 left
💡 Hint

Think about what coverage tells you about your tests and code.

Predict Output
intermediate
2:00remaining
What coverage percentage is shown after running these tests?

Given this simple Python function and tests, what coverage percentage will pytest-cov report?

PyTest
def add(a, b):
    if a > 0:
        return a + b
    else:
        return b


def test_add_positive():
    assert add(2, 3) == 5

# Note: no test for add with a <= 0
A75% coverage
B100% coverage
C50% coverage
D25% coverage
Attempts:
2 left
💡 Hint

Think about which lines run when add(2, 3) is called.

assertion
advanced
1:30remaining
Which assertion best checks test completeness using coverage data?

You have coverage data showing 85% line coverage. Which assertion best verifies your tests cover at least 80% of lines?

Aassert coverage_percent == 85
Bassert coverage_percent >= 80
Cassert coverage_percent < 80
Dassert coverage_percent > 85
Attempts:
2 left
💡 Hint

Think about verifying minimum coverage threshold.

🔧 Debug
advanced
2:00remaining
Why does this coverage report show 0% for a tested function?

Given this code and test, why does coverage show 0% for the function?

PyTest
def multiply(x, y):
    return x * y


def test_multiply():
    result = multiply(3, 4)
    assert result == 12

# Coverage run command: pytest --cov=module_name
# But coverage shows 0% for multiply function
AThe module name in the coverage command is incorrect or missing.
BCoverage only measures test execution time, not code run.
CThe multiply function has a syntax error.
DThe test assertion is wrong, so coverage is zero.
Attempts:
2 left
💡 Hint

Check if coverage is pointed to the right code file.

framework
expert
2:00remaining
How to configure pytest-cov to fail tests if coverage is below threshold?

Which pytest-cov option configuration will cause tests to fail if coverage is under 90%?

Apytest --cov=module --coverage-fail=90
Bpytest --cov=module --fail-under=90
Cpytest --cov=module --cov-fail-under=90
Dpytest --cov=module --fail-if-coverage-below=90
Attempts:
2 left
💡 Hint

Look for the official pytest-cov option to fail on low coverage.