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 |
request.addfinalizer in a pytest fixture?request.addfinalizerrequest?addfinalizer method expects a callable (function) without calling it immediately.import pytest
def cleanup():
print('Cleanup done')
@pytest.fixture
def resource(request):
request.addfinalizer(cleanup)
print('Setup done')
return 42
def test_example(resource):
print(f'Test got resource: {resource}')
assert resource == 42
request.addfinalizer:
@pytest.fixture
def my_fixture(request):
def cleanup():
print('Cleaning up')
request.addfinalizer(cleanup())
return 'data'
cleanup() immediately, passing its result (None) instead of the function itself.addfinalizer so it runs after the test.request.addfinalizer to close two resources after a test. Which code correctly ensures both cleanup functions run after the test?@pytest.fixture
def multi_resource(request):
def cleanup1():
print('Close resource 1')
def cleanup2():
print('Close resource 2')
request.addfinalizer(cleanup1)
request.addfinalizer(cleanup2)
return 'resources' correctly calls request.addfinalizer twice, passing function references without calling them.@pytest.fixture
def multi_resource(request):
def cleanup():
print('Close resource 1')
print('Close resource 2')
request.addfinalizer(cleanup())
return 'resources' calls cleanup immediately (wrong). @pytest.fixture
def multi_resource(request):
def cleanup1():
print('Close resource 1')
def cleanup2():
print('Close resource 2')
request.addfinalizer(cleanup1())
request.addfinalizer(cleanup2())
return 'resources' calls cleanup1() and cleanup2() immediately (wrong). @pytest.fixture
def multi_resource(request):
def cleanup1():
print('Close resource 1')
def cleanup2():
print('Close resource 2')
request.addfinalizer = cleanup1
request.addfinalizer = cleanup2
return 'resources' assigns functions to request.addfinalizer attribute, overwriting it (wrong).Pass the test and this pattern moves unknown