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.
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.
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
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | pytest session starts and session_scope fixture setup runs once | Session scope fixture is set up, printed 'Setup session scope' | - | PASS |
| 2 | module_scope fixture setup runs once for the module | Module scope fixture is set up, printed 'Setup module scope' | - | PASS |
| 3 | class_scope fixture setup runs once before TestExample tests | Class scope fixture is set up, printed 'Setup class scope' | - | PASS |
| 4 | test_one starts, func_scope fixture setup runs | Function scope fixture is set up, printed 'Setup function scope' | assert True passes | PASS |
| 5 | test_one ends, func_scope fixture teardown runs | Function scope fixture is torn down, printed 'Teardown function scope' | - | PASS |
| 6 | test_two starts, func_scope fixture setup runs | Function scope fixture is set up again, printed 'Setup function scope' | assert True passes | PASS |
| 7 | test_two ends, func_scope fixture teardown runs | Function scope fixture is torn down, printed 'Teardown function scope' | - | PASS |
| 8 | class_scope fixture teardown runs after all TestExample tests | Class scope fixture is torn down, printed 'Teardown class scope' | - | PASS |
| 9 | test_three starts, func_scope fixture setup runs | Function scope fixture is set up, printed 'Setup function scope' | assert True passes | PASS |
| 10 | test_three ends, func_scope fixture teardown runs | Function scope fixture is torn down, printed 'Teardown function scope' | - | PASS |
| 11 | module_scope fixture teardown runs after all tests in module | Module scope fixture is torn down, printed 'Teardown module scope' | - | PASS |
| 12 | session_scope fixture teardown runs after all tests in session | Session scope fixture is torn down, printed 'Teardown session scope' | - | PASS |