Complete worked example
python
import pytest
class Resource:
def __init__(self):
self.closed = False
def close(self):
self.closed = True
@pytest.fixture
def resource():
value = Resource()
try:
yield value
finally:
value.close()
def test_resource_is_open_during_call(resource):
assert resource.closed is False
def test_close_releases_resource():
value = Resource()
value.close()
assert value.closed is True- Reasoning
- The worked example uses a synthetic Resource with explicit close state. The fixture constructs it, yields it and closes it in finally. The test checks the resource is usable while the test runs. A separate unit test verifies that close changes the resource’s public state. The playground’s lifecycle panel separates setup, call and teardown reports. A green call with a red teardown still means the test session failed. This helps explain why a cleanup error can coexist with a successful assertion rather than being dismissed as unrelated terminal noise.