0
0
PyTesttesting~15 mins

Fixture scope (function, class, module, session) in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify pytest fixture scopes function, class, module, and session
Preconditions (2)
Step 1: Create a fixture with scope='function' that prints 'Setup function' when used
Step 2: Create a fixture with scope='class' that prints 'Setup class' when used
Step 3: Create a fixture with scope='module' that prints 'Setup module' when used
Step 4: Create a fixture with scope='session' that prints 'Setup session' when used
Step 5: Write test functions and test classes that use these fixtures
Step 6: Run pytest with the test file
Step 7: Observe the order and number of times each fixture setup message is printed
✅ Expected Result: The 'Setup function' message prints before each test function that uses the function-scoped fixture. The 'Setup class' message prints once per test class that uses the class-scoped fixture. The 'Setup module' message prints once per module. The 'Setup session' message prints only once for the entire test session.
Automation Requirements - pytest
Assertions Needed:
Verify that function-scoped fixture setup runs before each test function
Verify that class-scoped fixture setup runs once per test class
Verify that module-scoped fixture setup runs once per module
Verify that session-scoped fixture setup runs once per test session
Best Practices:
Use pytest fixtures with proper scope parameter
Use print statements or logging to track fixture setup calls
Use pytest's capsys fixture to capture output for assertions
Structure tests to clearly separate function, class, module, and session scope usage
Automated Solution
PyTest
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.

Common Mistakes - 4 Pitfalls
Using function scope fixture but expecting it to run only once
Not yielding in fixtures and missing teardown steps
{'mistake': 'Using print statements without capturing output in tests', 'why_bad': 'Print output is not captured by default, so assertions on printed messages fail.', 'correct_approach': "Use pytest's capsys fixture to capture and assert printed output."}
Defining fixtures inside test classes
Bonus Challenge

Now add data-driven testing with 3 different inputs to test functions using the function-scoped fixture

Show Hint