Fixture scope (function, class, module, session) in PyTest - Build an Automation Script
import pytest setup_calls = { 'function': 0, 'class': 0, 'module': 0, 'session': 0 } @pytest.fixture(scope='function') def func_scope_fixture(): setup_calls['function'] += 1 print('Setup function') yield @pytest.fixture(scope='class') def class_scope_fixture(): setup_calls['class'] += 1 print('Setup class') yield @pytest.fixture(scope='module') def module_scope_fixture(): setup_calls['module'] += 1 print('Setup module') yield @pytest.fixture(scope='session') def session_scope_fixture(): setup_calls['session'] += 1 print('Setup session') yield # Test functions using function and module scope def test_func1(func_scope_fixture, module_scope_fixture, session_scope_fixture): assert setup_calls['function'] >= 1 assert setup_calls['module'] == 1 assert setup_calls['session'] == 1 def test_func2(func_scope_fixture, module_scope_fixture, session_scope_fixture): assert setup_calls['function'] >= 2 assert setup_calls['module'] == 1 assert setup_calls['session'] == 1 # Test class using class and module scope class TestClass: def test_method1(self, class_scope_fixture, module_scope_fixture, session_scope_fixture): assert setup_calls['class'] == 1 assert setup_calls['module'] == 1 assert setup_calls['session'] == 1 def test_method2(self, class_scope_fixture, module_scope_fixture, session_scope_fixture): assert setup_calls['class'] == 1 assert setup_calls['module'] == 1 assert setup_calls['session'] == 1
This test script defines four fixtures with different scopes: function, class, module, and session.
Each fixture increments a counter and prints a setup message when it runs.
Test functions and a test class use these fixtures to verify how many times each setup runs.
Assertions check that function-scoped fixture runs before each test function, class-scoped fixture runs once per class, module-scoped fixture runs once per module, and session-scoped fixture runs once per entire test session.
This approach uses counters and assertions to confirm fixture scope behavior clearly and simply.
Now add data-driven testing with 3 different inputs to test functions using the function-scoped fixture
Practice
function scope mean for a pytest fixture?Solution
Step 1: Understand fixture scopes
Pytest fixtures can have different scopes controlling how often they run.Step 2: Define function scope meaning
Function scope means the fixture runs before each test function and tears down after it.Final Answer:
The fixture runs once per test function. -> Option DQuick Check:
function scope = runs per test function [OK]
- Confusing function scope with class or module scope
- Thinking function scope runs once per class
- Assuming function scope runs once per session
module scope?Solution
Step 1: Recall fixture scope syntax
To set a fixture scope, use @pytest.fixture(scope='scope_name').Step 2: Match scope name for module
The correct scope name for module is 'module'.Final Answer:
@pytest.fixture(scope='module') -> Option AQuick Check:
module scope syntax = scope='module' [OK]
- Using 'session' instead of 'module' for module scope
- Forgetting to add scope parameter
- Using incorrect quotes or misspelling scope
setup_db run?
import pytest
@pytest.fixture(scope='class')
def setup_db():
print('Setup DB')
class TestA:
def test_1(self, setup_db):
pass
def test_2(self, setup_db):
pass
class TestB:
def test_3(self, setup_db):
pass
Solution
Step 1: Understand class scope behavior
Fixture with class scope runs once per test class, before any tests in that class.Step 2: Count test classes using the fixture
There are two test classes: TestA and TestB, each uses setup_db fixture.Step 3: Calculate total runs
setup_db runs once for TestA and once for TestB, total 2 times.Final Answer:
2 times -> Option CQuick Check:
class scope = once per class, 2 classes = 2 runs [OK]
- Counting runs per test method instead of per class
- Confusing class scope with function scope
- Ignoring multiple test classes
import pytest
@pytest.fixture(scope='modul')
def setup_env():
pass
def test_example(setup_env):
assert True
Solution
Step 1: Check fixture scope spelling
The scope value 'modul' is misspelled; correct is 'module'.Step 2: Understand effect of typo
Pytest will not recognize the scope and may default or error, causing fixture not to run properly.Final Answer:
Typo in scope value causes fixture not to run. -> Option BQuick Check:
Scope must be spelled correctly [OK]
- Assuming fixture must return a value
- Confusing fixture decorator with marker
- Ignoring spelling errors in parameters
Solution
Step 1: Understand the requirement
The database connection should be initialized once before all tests in multiple modules and closed after all tests.Step 2: Match scope to requirement
Session scope runs once per entire test session, perfect for setup/teardown across all modules.Final Answer:
Usesessionscope to share across all tests in the run. -> Option AQuick Check:
Session scope = once per test run [OK]
- Choosing function or class scope which run too often
- Using module scope which limits to one file
- Not matching scope to resource lifetime
Ready to prove it?
Pass the test and this pattern moves unknown
