Fixture scope controls how often a setup code runs in tests. It helps save time by reusing setup steps when possible.
0
0
Fixture scope (function, class, module, session) in PyTest
Introduction
When you want to run setup before each test function to keep tests independent.
When you want to run setup once per test class to share setup among its tests.
When you want to run setup once per module to share setup among all tests in a file.
When you want to run setup once per test session to share setup across all tests in a run.
When you want to speed up tests by avoiding repeated setup steps.
Syntax
PyTest
@pytest.fixture(scope='function' or 'class' or 'module' or 'session') def fixture_name(): # setup code yield resource # teardown code
The scope parameter defines how often the fixture runs.
Default scope is function, meaning it runs before each test function.
Examples
Runs setup and teardown before and after each test function.
PyTest
@pytest.fixture(scope='function') def setup_function(): print('Setup for each test') yield print('Teardown for each test')
Runs setup once before all tests in a class and teardown after all tests in that class.
PyTest
@pytest.fixture(scope='class') def setup_class(): print('Setup once per test class') yield print('Teardown once per test class')
Runs setup once before all tests in a file and teardown after all tests in that file.
PyTest
@pytest.fixture(scope='module') def setup_module(): print('Setup once per module') yield print('Teardown once per module')
Runs setup once before all tests in the entire run and teardown after all tests finish.
PyTest
@pytest.fixture(scope='session') def setup_session(): print('Setup once per test session') yield print('Teardown once per test session')
Sample Program
This test shows two fixtures with different scopes. The function-scoped fixture runs before and after each test. The class-scoped fixture runs once before all tests in the class and once after.
PyTest
import pytest @pytest.fixture(scope='function') def resource_function(): print('Setup function') yield 'resource' print('Teardown function') @pytest.fixture(scope='class') def resource_class(): print('Setup class') yield 'resource' print('Teardown class') class TestExample: def test_one(self, resource_function, resource_class): print('Test one running') assert resource_function == 'resource' assert resource_class == 'resource' def test_two(self, resource_function, resource_class): print('Test two running') assert resource_function == 'resource' assert resource_class == 'resource'
OutputSuccess
Important Notes
Use function scope for isolated tests that need fresh setup each time.
Use wider scopes like class, module, or session to speed up tests by sharing setup.
Remember teardown runs in reverse order of setup.
Summary
Fixture scope controls how often setup and teardown run.
Scopes: function (each test), class (each test class), module (each file), session (whole run).
Choosing the right scope saves time and keeps tests reliable.