0
0
PyTesttesting~20 mins

Why parametrize multiplies test coverage in PyTest - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parametrize Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does pytest parametrize increase test coverage?

Consider a test function that uses @pytest.mark.parametrize to run multiple input values. Why does this approach multiply test coverage?

AIt skips tests that are redundant, reducing coverage but saving time.
BIt runs tests in parallel, speeding up execution but not increasing coverage.
CIt automatically generates new test functions from the code under test.
DIt runs the same test logic multiple times with different inputs, covering more cases.
Attempts:
2 left
💡 Hint

Think about how running a test with different inputs affects the number of scenarios checked.

Predict Output
intermediate
2:00remaining
Output of parametrized pytest test count

Given the following pytest test code, how many tests will be executed?

PyTest
import pytest

@pytest.mark.parametrize('x', [1, 2])
@pytest.mark.parametrize('y', [3, 4, 5])
def test_sum(x, y):
    assert x + y > 0
A6
B5
C3
D2
Attempts:
2 left
💡 Hint

Multiply the number of values in each parametrize decorator.

assertion
advanced
2:00remaining
Correct assertion for parametrized test coverage

Which assertion correctly verifies that a parametrized test ran all expected cases?

PyTest
import pytest

@pytest.mark.parametrize('val', [10, 20, 30])
def test_positive(val):
    assert val > 0
Aassert pytest.main(['-v']).count('test_positive') == 3
Bassert pytest.main(['-v']).count('test_positive') == 0
Cassert pytest.main(['-v']).count('test_positive') == 1
Dassert pytest.main(['-v']).count('test_positive') == 6
Attempts:
2 left
💡 Hint

Count how many times the test function name appears in verbose output.

🔧 Debug
advanced
2:00remaining
Identify the error in parametrized test coverage

Why does this parametrized test not increase coverage as expected?

PyTest
import pytest

@pytest.mark.parametrize('a', [1, 2])
def test_example(a):
    assert a != 0

@pytest.mark.parametrize('b', [3, 4])
def test_example(b):
    assert b != 0
ABoth tests run with 4 total cases, coverage is correct.
BThe second test_example overwrites the first, so only 2 tests run.
CParametrize decorators must be combined on one function to multiply coverage.
DThe test names must be different to run both parametrized tests.
Attempts:
2 left
💡 Hint

Check if function names are unique in the test file.

framework
expert
2:00remaining
How does pytest parametrize multiply coverage internally?

Which mechanism does pytest use internally to multiply test coverage when using @pytest.mark.parametrize?

AIt runs the test once and caches results for all inputs.
BIt modifies the test function code to loop over inputs during execution.
CIt generates multiple test function calls with different arguments at collection time.
DIt duplicates the test file for each parameter set before running tests.
Attempts:
2 left
💡 Hint

Think about when pytest creates separate test cases for each parameter.