0
0
PyTesttesting~10 mins

Fixture scope (function, class, module, session) in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how pytest fixtures with different scopes (function, class, module, session) are set up and used. It verifies that fixtures are created and destroyed according to their scope during test execution.

Test Code - pytest
PyTest
import pytest

@pytest.fixture(scope="function")
def func_scope():
    print("Setup function scope")
    yield
    print("Teardown function scope")

@pytest.fixture(scope="class")
def class_scope():
    print("Setup class scope")
    yield
    print("Teardown class scope")

@pytest.fixture(scope="module")
def module_scope():
    print("Setup module scope")
    yield
    print("Teardown module scope")

@pytest.fixture(scope="session")
def session_scope():
    print("Setup session scope")
    yield
    print("Teardown session scope")

class TestExample:
    def test_one(self, func_scope, class_scope, module_scope, session_scope):
        assert True

    def test_two(self, func_scope, class_scope, module_scope, session_scope):
        assert True

def test_three(func_scope, module_scope, session_scope):
    assert True
Execution Trace - 12 Steps
StepActionSystem StateAssertionResult
1pytest session starts and session_scope fixture setup runs onceSession scope fixture is set up, printed 'Setup session scope'-PASS
2module_scope fixture setup runs once for the moduleModule scope fixture is set up, printed 'Setup module scope'-PASS
3class_scope fixture setup runs once before TestExample testsClass scope fixture is set up, printed 'Setup class scope'-PASS
4test_one starts, func_scope fixture setup runsFunction scope fixture is set up, printed 'Setup function scope'assert True passesPASS
5test_one ends, func_scope fixture teardown runsFunction scope fixture is torn down, printed 'Teardown function scope'-PASS
6test_two starts, func_scope fixture setup runsFunction scope fixture is set up again, printed 'Setup function scope'assert True passesPASS
7test_two ends, func_scope fixture teardown runsFunction scope fixture is torn down, printed 'Teardown function scope'-PASS
8class_scope fixture teardown runs after all TestExample testsClass scope fixture is torn down, printed 'Teardown class scope'-PASS
9test_three starts, func_scope fixture setup runsFunction scope fixture is set up, printed 'Setup function scope'assert True passesPASS
10test_three ends, func_scope fixture teardown runsFunction scope fixture is torn down, printed 'Teardown function scope'-PASS
11module_scope fixture teardown runs after all tests in moduleModule scope fixture is torn down, printed 'Teardown module scope'-PASS
12session_scope fixture teardown runs after all tests in sessionSession scope fixture is torn down, printed 'Teardown session scope'-PASS
Failure Scenario
Failing Condition: Fixture setup or teardown fails or fixture is not found
Execution Trace Quiz - 3 Questions
Test your understanding
Which fixture scope causes the setup to run once per test function?
Aclass
Bfunction
Cmodule
Dsession
Key Result
Use fixture scopes to control how often setup and teardown code runs, improving test efficiency and clarity.