Complete the code to define a simple pytest fixture.
import pytest @pytest.fixture def [1](): return 42
The fixture name must be a valid Python function name. Here, simple_fixture is a clear and valid name.
Complete the code to use a fixture in a test function.
def test_answer([1]): assert [1] == 42
The test function must accept the fixture name as a parameter to use it. Here, simple_fixture matches the fixture defined earlier.
Fix the error in the fixture that uses yield for setup and teardown.
import pytest @pytest.fixture def resource(): print('Setup') [1] 10 print('Teardown')
return instead of yield in fixtures needing teardown.yield.Using yield in a fixture allows setup before the yield and teardown after. return would end the function immediately without teardown.
Fill both blanks to create a fixture that depends on another fixture and modifies its value.
@pytest.fixture def modified_resource([1]): value = [2] + 5 return value
The fixture modified_resource depends on the resource fixture. It receives resource as a parameter and adds 5 to it.
Fill all three blanks to create a parametrized fixture that runs tests with multiple inputs.
@pytest.fixture(params=[1, 2, 3]) def param_fixture([1]): return [2] def test_param([3]): assert [3] > 0
params as a parameter name in the fixture function.request.param to get the current parameter.The fixture uses request to access the current parameter with request.param. The test function receives the fixture param_fixture as a parameter.