0
0
PyTesttesting~10 mins

Why advanced fixtures handle complex scenarios in PyTest - Test Your Understanding

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

Complete the code to define a simple pytest fixture.

PyTest
import pytest

@pytest.fixture
def [1]():
    return 42
Drag options to blanks, or click blank then click option'
Afixture_func
Bsimple_fixture
Csetup
Dtest_func
Attempts:
3 left
💡 Hint
Common Mistakes
Using names that start with 'test_' which pytest treats as test functions.
Using invalid Python names or reserved keywords.
2fill in blank
medium

Complete the code to use a fixture in a test function.

PyTest
def test_answer([1]):
    assert [1] == 42
Drag options to blanks, or click blank then click option'
Asimple_fixture
Bsetup
Cfixture_func
Danswer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the fixture name.
Forgetting to include the fixture as a parameter.
3fill in blank
hard

Fix the error in the fixture that uses yield for setup and teardown.

PyTest
import pytest

@pytest.fixture
def resource():
    print('Setup')
    [1] 10
    print('Teardown')
Drag options to blanks, or click blank then click option'
Areturn
Bbreak
Cyield
Dpass
Attempts:
3 left
💡 Hint
Common Mistakes
Using return instead of yield in fixtures needing teardown.
Placing teardown code before yield.
4fill in blank
hard

Fill both blanks to create a fixture that depends on another fixture and modifies its value.

PyTest
@pytest.fixture
def modified_resource([1]):
    value = [2] + 5
    return value
Drag options to blanks, or click blank then click option'
Aresource
Csimple_fixture
Dsetup
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixture name that does not exist.
Not passing the dependent fixture as a parameter.
5fill in blank
hard

Fill all three blanks to create a parametrized fixture that runs tests with multiple inputs.

PyTest
@pytest.fixture(params=[1, 2, 3])
def param_fixture([1]):
    return [2]

def test_param([3]):
    assert [3] > 0
Drag options to blanks, or click blank then click option'
Arequest
Brequest.param
Cparam_fixture
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using params as a parameter name in the fixture function.
Not using request.param to get the current parameter.
Not passing the fixture to the test function.