Challenge - 5 Problems
Conditional Parametrize Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of conditionally parametrized pytest test
Given the following pytest test code with conditional parametrization, what will be the output when running the test?
import pytest
@pytest.mark.parametrize("input,expected", [
(1, 2),
(2, 3),
pytest.param(3, 4, marks=pytest.mark.skipif(True, reason="skip 3")),
])
def test_increment(input, expected):
assert input + 1 == expected
PyTest
import pytest @pytest.mark.parametrize("input,expected", [ (1, 2), (2, 3), pytest.param(3, 4, marks=pytest.mark.skipif(True, reason="skip 3")), ]) def test_increment(input, expected): assert input + 1 == expected
Attempts:
2 left
💡 Hint
Look at the skipif mark condition and how it affects test execution.
✗ Incorrect
The third test case is marked to be skipped unconditionally (skipif(True)). So pytest will run only the first two tests which pass.
❓ assertion
intermediate2:00remaining
Assertion outcome with conditional parametrize
Consider this pytest test with conditional parametrization based on a variable:
What will happen when running this test?
import pytest
run_extra = False
params = [(1, 2), (2, 3)]
if run_extra:
params.append((3, 5))
@pytest.mark.parametrize("input,expected", params)
def test_add_one(input, expected):
assert input + 1 == expected
What will happen when running this test?
PyTest
import pytest run_extra = False params = [(1, 2), (2, 3)] if run_extra: params.append((3, 5)) @pytest.mark.parametrize("input,expected", params) def test_add_one(input, expected): assert input + 1 == expected
Attempts:
2 left
💡 Hint
Check the value of run_extra and how it affects params.
✗ Incorrect
Since run_extra is False, only two test cases are parametrized and both pass.
🔧 Debug
advanced2:00remaining
Identify the error in conditional parametrize usage
What error will this pytest code raise?
import pytest
condition = False
@pytest.mark.parametrize("x", [1, 2] if condition else 3)
def test_example(x):
assert x > 0
PyTest
import pytest condition = False @pytest.mark.parametrize("x", [1, 2] if condition else 3) def test_example(x): assert x > 0
Attempts:
2 left
💡 Hint
Check the else part of the conditional expression.
✗ Incorrect
The else part returns an int (3), which is not iterable. parametrize expects an iterable of values.
❓ framework
advanced2:00remaining
Best way to conditionally parametrize tests in pytest
You want to run a test with extra parameters only if an environment variable TEST_EXTRA is set to 'yes'. Which pytest approach correctly implements this conditional parametrization?
Options:
Options:
PyTest
import os import pytest extra_params = [(3, 4)] if os.getenv('TEST_EXTRA') == 'yes' else [] params = [(1, 2), (2, 3)] + extra_params @pytest.mark.parametrize("input,expected", params) def test_func(input, expected): assert input + 1 == expected
Attempts:
2 left
💡 Hint
Think about simple and clear ways to add parameters conditionally.
✗ Incorrect
Building the parameter list conditionally before decorating is the simplest and most readable approach.
🧠 Conceptual
expert2:00remaining
Understanding conditional parametrize impact on test count
If you have this pytest code:
How many tests will pytest run?
import pytest
@pytest.mark.parametrize("a", [1, 2])
@pytest.mark.parametrize("b", [3, 4] if False else [5])
def test_combined(a, b):
assert a < 10 and b < 10
How many tests will pytest run?
PyTest
import pytest @pytest.mark.parametrize("a", [1, 2]) @pytest.mark.parametrize("b", [3, 4] if False else [5]) def test_combined(a, b): assert a < 10 and b < 10
Attempts:
2 left
💡 Hint
Remember how multiple parametrize decorators combine parameters.
✗ Incorrect
The outer parametrize with b has only one value [5]. The inner parametrize with a has two values [1,2]. Total tests = 2 * 1 = 2.