0
0
PyTesttesting~20 mins

Conditional parametrize in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Parametrize Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AAll three tests pass
BTwo tests pass, one test fails
CTwo tests pass, one test is skipped
DAll three tests fail
Attempts:
2 left
💡 Hint
Look at the skipif mark condition and how it affects test execution.
assertion
intermediate
2:00remaining
Assertion outcome with conditional parametrize
Consider this pytest test with conditional parametrization based on a variable:

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
ATwo tests run and both pass
BThree tests run, one fails
CTwo tests run, one fails
DThree tests run and all pass
Attempts:
2 left
💡 Hint
Check the value of run_extra and how it affects params.
🔧 Debug
advanced
2: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
ATypeError: 'int' object is not iterable
BSyntaxError: invalid syntax
CNo error, tests run with x=1 and x=2
DValueError: parametrize values must be a list or tuple
Attempts:
2 left
💡 Hint
Check the else part of the conditional expression.
framework
advanced
2: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:
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
AUse pytest hooks to add parameters dynamically at runtime
BUse pytest.mark.skipif inside parametrize for each extra param
CUse pytest parametrize with indirect=True and check env inside test
DUse a conditional list to build params before @pytest.mark.parametrize as shown
Attempts:
2 left
💡 Hint
Think about simple and clear ways to add parameters conditionally.
🧠 Conceptual
expert
2:00remaining
Understanding conditional parametrize impact on test count
If you have this pytest code:

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
A4 tests
B2 tests
C1 test
D3 tests
Attempts:
2 left
💡 Hint
Remember how multiple parametrize decorators combine parameters.