Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a lazy fixture using pytest.
PyTest
import pytest @pytest.fixture def [1](): return 42
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid Python identifiers as fixture names.
Forgetting the @pytest.fixture decorator.
✗ Incorrect
The fixture name can be any valid identifier. Here, 'my_fixture' is a valid fixture name.
2fill in blank
mediumComplete the code to use a lazy fixture inside a test function.
PyTest
def test_example([1]): assert [1] == 42
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the fixture name in the test function parameter.
Forgetting to include the fixture as a parameter.
✗ Incorrect
The test function parameter must match the fixture name to use it.
3fill in blank
hardFix the error in the fixture definition to make it lazy.
PyTest
import pytest @pytest.fixture(scope='[1]') def my_fixture(): return 42
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid scope like 'lazy'.
Confusing fixture scope with other pytest parameters.
✗ Incorrect
The default and correct scope for lazy fixtures is 'function', which means the fixture is called lazily for each test function.
4fill in blank
hardFill both blanks to use a lazy fixture with indirect parametrization.
PyTest
@pytest.mark.parametrize('input', [1, 2], indirect=[1]) def test_lazy([2]): assert input > 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting indirect to False or a wrong value.
Using a test function parameter name different from the parametrize argument.
✗ Incorrect
The 'indirect' parameter must be True to enable lazy fixture usage, and the test function parameter must match the fixture name 'input'.
5fill in blank
hardFill all three blanks to correctly use lazy_fixture in a parametrized test.
PyTest
import pytest from pytest_lazyfixture import lazy_fixture @pytest.mark.parametrize('data', [[1], [2], [3]]) def test_data(data): assert data in [10, 20] @pytest.fixture def fixture_a(): return 10 @pytest.fixture def fixture_b(): return 20
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixture names directly instead of lazy_fixture calls in parametrize.
Not importing lazy_fixture from pytest_lazyfixture.
✗ Incorrect
Use lazy_fixture('fixture_a') and lazy_fixture('fixture_b') in parametrize to refer to fixtures lazily. The test function parameter 'data' receives the fixture value.