0
0
PyTesttesting~20 mins

Parametrized fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parametrized Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a parametrized fixture with multiple values
What will be the output when running this pytest test with the given parametrized fixture?
PyTest
import pytest

@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.param

def test_is_positive(number):
    assert number > 0
    print(f"Testing with number: {number}")
ATest runs once printing: Testing with number: [1, 2, 3]
BTest runs 3 times printing: Testing with number: 1, Testing with number: 2, Testing with number: 3
CTest runs 3 times but fails on number 1
DTest runs 1 time and fails because number is not defined
Attempts:
2 left
💡 Hint
Remember that pytest runs the test once for each parameter in the fixture.
assertion
intermediate
2:00remaining
Correct assertion for a parametrized fixture test
Given this parametrized fixture, which assertion correctly verifies the fixture value is even?
PyTest
import pytest

@pytest.fixture(params=[2, 4, 5])
def even_number(request):
    return request.param

def test_even(even_number):
    # Which assertion is correct here?
Aassert even_number % 2 == 0
Bassert even_number % 2 != 0
Cassert even_number == 5
Dassert even_number > 5
Attempts:
2 left
💡 Hint
An even number is divisible by 2 with no remainder.
🔧 Debug
advanced
2:00remaining
Identify the error in this parametrized fixture usage
What error will this pytest code raise when executed?
PyTest
import pytest

@pytest.fixture(params=["a", "b", "c"])
def letter(request):
    return letter

def test_letter(letter):
    assert letter in ['a', 'b', 'c']
AAssertionError because letter is not in the list
BTypeError because 'letter' is not iterable
CRecursionError due to fixture returning itself
DNo error, test passes
Attempts:
2 left
💡 Hint
Check what the fixture returns inside its function body.
framework
advanced
2:00remaining
Using indirect parametrization with fixtures
Which option correctly uses indirect parametrization to pass parameters to a fixture?
PyTest
import pytest

@pytest.fixture
def data(request):
    return request.param * 2

def test_data(data):
    assert data in [2, 4, 6]
A
@pytest.mark.parametrize('data', [1, 2, 3])
def test_data(data):
    assert data in [2, 4, 6]
B
@pytest.mark.parametrize('data', [2, 4, 6], indirect=False)
def test_data(data):
    assert data in [2, 4, 6]
C
@pytest.mark.parametrize('request', [1, 2, 3], indirect=True)
def test_data(data):
    assert data in [2, 4, 6]
D
@pytest.mark.parametrize('data', [1, 2, 3], indirect=True)
def test_data(data):
    assert data in [2, 4, 6]
Attempts:
2 left
💡 Hint
Indirect parametrization tells pytest to pass parameters to the fixture, not directly to the test.
🧠 Conceptual
expert
2:00remaining
Why use parametrized fixtures instead of simple parametrized tests?
Which is the best explanation for using parametrized fixtures over parametrized tests in pytest?
AParametrized fixtures allow sharing setup code and parameters across multiple tests, improving code reuse and clarity.
BParametrized fixtures run tests faster by skipping setup steps.
CParametrized fixtures prevent tests from running multiple times.
DParametrized fixtures automatically generate test reports without assertions.
Attempts:
2 left
💡 Hint
Think about how fixtures help organize setup and data for tests.