Challenge - 5 Problems
Parametrize Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple parametrize test
What will be the output when running this pytest test with @pytest.mark.parametrize?
PyTest
import pytest @pytest.mark.parametrize('x,y', [(1,2), (3,4)]) def test_sum(x, y): assert x + y == 3
Attempts:
2 left
💡 Hint
Check the sum of each pair and compare with 3
✗ Incorrect
The first pair (1,2) sums to 3, so the assertion passes. The second pair (3,4) sums to 7, so the assertion fails.
❓ assertion
intermediate2:00remaining
Correct assertion for parametrize test
Which assertion correctly tests that the product of x and y equals 6 in this parametrize test?
PyTest
import pytest @pytest.mark.parametrize('x,y', [(1,6), (2,3), (3,2)]) def test_product(x, y): # Choose the correct assertion below
Attempts:
2 left
💡 Hint
Product means multiplication
✗ Incorrect
The test checks if multiplying x and y equals 6. Only option A uses multiplication correctly.
🔧 Debug
advanced2:00remaining
Identify the error in parametrize usage
What error will this pytest code raise when run?
PyTest
import pytest @pytest.mark.parametrize('x,y', [(1, 2), (3, 4)]) def test_values(x, y): assert x < y
Attempts:
2 left
💡 Hint
Check the format of the list passed to parametrize
✗ Incorrect
The list should contain tuples of two values, but here it contains integers, so unpacking fails.
🧠 Conceptual
advanced2:00remaining
Purpose of @pytest.mark.parametrize
What is the main purpose of using @pytest.mark.parametrize in pytest tests?
Attempts:
2 left
💡 Hint
Think about repeating tests with different data
✗ Incorrect
Parametrize allows running one test function many times with different inputs to check multiple cases easily.
❓ framework
expert2:00remaining
Combining parametrize with fixtures
Given this pytest code, what will be the output when running the test?
PyTest
import pytest @pytest.fixture def base_value(): return 10 @pytest.mark.parametrize('increment', [1, 2]) def test_increment(base_value, increment): assert base_value + increment in [11, 12]
Attempts:
2 left
💡 Hint
Check the sum of base_value and increment for each param
✗ Incorrect
The fixture returns 10. Adding increments 1 and 2 gives 11 and 12, both in the allowed list, so both tests pass.