Test Overview
This test uses a pytest fixture with request.addfinalizer to run cleanup code after the test finishes. It verifies that the finalizer runs and changes a variable as expected.
This test uses a pytest fixture with request.addfinalizer to run cleanup code after the test finishes. It verifies that the finalizer runs and changes a variable as expected.
import pytest @pytest.fixture def resource(request): state = {"cleaned": False} def cleanup(): state["cleaned"] = True request.addfinalizer(cleanup) return state def test_resource_cleanup(resource): assert resource["cleaned"] is False # After test ends, finalizer sets resource["cleaned"] to True
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | - | PASS |
| 2 | Fixture 'resource' is called with 'request' parameter | Fixture creates state dictionary {'cleaned': False} | - | PASS |
| 3 | Fixture adds finalizer function 'cleanup' using request.addfinalizer | Finalizer registered to set state['cleaned'] = True after test | - | PASS |
| 4 | Test function 'test_resource_cleanup' runs with fixture 'resource' | state['cleaned'] is False at test start | Assert resource['cleaned'] is False | PASS |
| 5 | Test function completes | Test function finished without errors | - | PASS |
| 6 | pytest runs finalizer 'cleanup' registered by fixture | state['cleaned'] is set to True by finalizer | - | PASS |
| 7 | Test ends with all assertions passed and finalizer executed | Test environment cleaned up | - | PASS |