Complete the code to set a fixture with function scope.
import pytest @pytest.fixture(scope=[1]) def sample_fixture(): return 42
The default fixture scope is function, which means the fixture is called once per test function.
Complete the code to set a fixture with class scope.
import pytest @pytest.fixture(scope=[1]) def class_fixture(): return 'class level resource'
Class scope means the fixture is created once per test class and shared among all test methods in that class.
Fix the error in the fixture scope to make it run once per module.
import pytest @pytest.fixture(scope=[1]) def module_fixture(): return 'module resource'
Module scope means the fixture is created once per Python module and shared among all tests in that module.
Fill both blanks to create a session-scoped fixture and use it in a test.
import pytest @pytest.fixture(scope=[1]) def session_fixture(): return 'session resource' def test_example([2]): assert session_fixture == 'session resource'
The fixture is declared with session scope to run once per test session. The test function must accept the fixture name session_fixture as a parameter to use it.
Fill all three blanks to create a module-scoped fixture, use it in a test, and assert its value.
import pytest @pytest.fixture(scope=[1]) def mod_fixture(): return 100 def test_mod([2]): assert [3] == 100
The fixture has module scope, so it runs once per module. The test function uses the fixture by naming it mod_fixture as a parameter, and asserts that the fixture value equals 100.