Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The 'indirect=True' argument tells pytest to treat the parameter 'data' as a fixture and pass the values to it.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The fixture receives the 'request' object which contains the parameter in 'request.param'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The 'indirect=True' argument is required to tell pytest to use the fixture 'config' with the parameter values.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'indirect=False' disables indirect fixture usage.
Using 'scope' instead of 'indirect' causes errors.
✗ Incorrect
To parametrize multiple fixtures indirectly, use 'indirect=True' in parametrize.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not accepting 'request' in the fixture causes errors.
Omitting 'indirect=True' disables fixture parameterization.
✗ Incorrect
The fixture must accept 'request' to access 'request.param'. The parametrize decorator uses 'indirect=True' to apply parameters to the fixture.