0
0
PyTesttesting~10 mins

Lazy 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 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'
Adata_fixture
Blazy_fixture
Csetup_data
Dmy_fixture
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid Python identifiers as fixture names.
Forgetting the @pytest.fixture decorator.
2fill in blank
medium

Complete 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'
Asetup_data
Blazy_fixture
Cmy_fixture
Ddata_fixture
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.
3fill in blank
hard

Fix 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'
Afunction
Bsession
Cmodule
Dlazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid scope like 'lazy'.
Confusing fixture scope with other pytest parameters.
4fill in blank
hard

Fill 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'
ATrue
BFalse
Cinput
Dmy_fixture
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.
5fill in blank
hard

Fill 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'
Alazy_fixture('fixture_a')
Blazy_fixture('fixture_b')
Cfixture_a
Dfixture_b
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixture names directly instead of lazy_fixture calls in parametrize.
Not importing lazy_fixture from pytest_lazyfixture.