Challenge - 5 Problems
Parametrize Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined parametrize decorators in pytest
What is the total number of test cases executed by pytest for the following test function?
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
Attempts:
2 left
💡 Hint
Remember that multiple parametrize decorators multiply the number of test cases.
✗ Incorrect
pytest runs the test for every combination of parameters. Here, x has 2 values and y has 3 values, so total tests = 2 * 3 = 6.
❓ assertion
intermediate2:00remaining
Assertion behavior with combined parametrize decorators
Given the following test, which assertion will fail during pytest execution?
PyTest
import pytest @pytest.mark.parametrize('a', [0, 1]) @pytest.mark.parametrize('b', [2, 3]) def test_multiply(a, b): assert (a * b) != 0
Attempts:
2 left
💡 Hint
Check when the product of a and b equals zero.
✗ Incorrect
The assertion fails whenever a=0, since 0 * b = 0. This happens for a=0, b=2 (B) and a=0, b=3 (D).
🔧 Debug
advanced2:00remaining
Identify the error in combined parametrize usage
What error will pytest raise when running this test code?
PyTest
import pytest @pytest.mark.parametrize('x', [1, 2]) @pytest.mark.parametrize('x', [3, 4]) def test_values(x): assert x > 0
Attempts:
2 left
💡 Hint
Consider how pytest handles multiple parametrize decorators with the same argument name.
✗ Incorrect
pytest raises ValueError because the same argument 'x' is parametrized in multiple decorators (parameter names must be disjoint across decorators).
🧠 Conceptual
advanced2:00remaining
Order of parameter combinations with multiple parametrize decorators
In pytest, when combining multiple parametrize decorators, which statement about the order of test case generation is true?
Attempts:
2 left
💡 Hint
Think about nested loops and which loop runs inside the other.
✗ Incorrect
pytest applies parametrize decorators like nested loops: the inner decorator's parameters vary faster (like the inner loop).
❓ framework
expert3:00remaining
Combining parametrize with indirect fixture in pytest
Given the following pytest code, how many test cases will be executed?
PyTest
import pytest @pytest.fixture def data(request): return request.param * 2 @pytest.mark.parametrize('data', [1, 2], indirect=True) @pytest.mark.parametrize('flag', [True, False]) def test_indirect(data, flag): assert isinstance(data, int) and isinstance(flag, bool)
Attempts:
2 left
💡 Hint
Consider how indirect parametrize and multiple decorators combine.
✗ Incorrect
The test runs for each combination of data (indirectly parametrized with 2 values) and flag (2 values), total 2*2=4 tests.