0
0
PyTesttesting~10 mins

Parametrize with indirect fixtures in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parametrize the test with indirect fixture usage.

PyTest
import pytest

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

@pytest.mark.parametrize('data', [1, 2, 3], [1]=True)
def test_example(data):
    assert data in [1, 2, 3]
Drag options to blanks, or click blank then click option'
Aparams
Bscope
Cids
Dindirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'params' instead of 'indirect' causes pytest to treat values as direct parameters.
Forgetting to set indirect=True means the fixture is not used.
2fill in blank
medium

Complete the code to access the parameter inside the fixture using the request object.

PyTest
import pytest

@pytest.fixture
def setup_data([1]):
    return request.param * 2

@pytest.mark.parametrize('setup_data', [4, 5], indirect=True)
def test_double(setup_data):
    assert setup_data in [8, 10]
Drag options to blanks, or click blank then click option'
Aparam
Brequest
Cdata
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'param' or 'data' as argument instead of 'request' causes errors.
Trying to access 'param' without the request object.
3fill in blank
hard

Fix the error in the test by correctly marking the parameter for indirect fixture usage.

PyTest
import pytest

@pytest.fixture
def config(request):
    return request.param + 1

@pytest.mark.parametrize('config', [10, 20], [1]=True)
def test_config(config):
    assert config in [11, 21]
Drag options to blanks, or click blank then click option'
Aids
Bscope
Cindirect
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'indirect=True' causes the test to fail because the fixture is not used.
Using 'params' instead of 'indirect' causes wrong behavior.
4fill in blank
hard

Fill both blanks to parametrize two fixtures indirectly.

PyTest
import pytest

@pytest.fixture
def user(request):
    return request.param

@pytest.fixture
def role(request):
    return request.param

@pytest.mark.parametrize('user, role', [('alice', 'admin'), ('bob', 'user')], [1]=[2])
def test_access(user, role):
    assert user in ['alice', 'bob']
    assert role in ['admin', 'user']
Drag options to blanks, or click blank then click option'
Aindirect
BTrue
CFalse
Dscope
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'indirect=False' disables indirect fixture usage.
Using 'scope' instead of 'indirect' causes errors.
5fill in blank
hard

Fill all three blanks to parametrize a test with indirect fixtures and access the parameter inside the fixture.

PyTest
import pytest

@pytest.fixture
def item([1]):
    return request.param * 3

@pytest.mark.parametrize('item', [2, 3], [2]=[3])
def test_item(item):
    assert item in [6, 9]
Drag options to blanks, or click blank then click option'
Arequest
Bindirect
CTrue
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Not accepting 'request' in the fixture causes errors.
Omitting 'indirect=True' disables fixture parameterization.