Skip to content

✅ Fix order-dependent tests in test_dependency_contextmanager.py - #16368

Open
meagoodboy wants to merge 1 commit into
fastapi:masterfrom
meagoodboy:fix/order-dependent-contextmanager-tests
Open

meagoodboy wants to merge 1 commit into
fastapi:masterfrom
meagoodboy:fix/order-dependent-contextmanager-tests

Conversation

@meagoodboy

Copy link
Copy Markdown

tests/test_dependency_contextmanager.py keeps a module-level state dict (and an errors list) that the app's yield dependencies mutate in place. Four tests open by asserting the pristine value, so they only pass if nothing else in the file ran first. The file passes in definition order but any other order breaks it:

$ pytest tests/test_dependency_contextmanager.py::test_sync_async_state tests/test_dependency_contextmanager.py::test_async_state
def test_async_state():
>       assert state["/async"] == "asyncgen not started"
E       AssertionError: assert 'asyncgen completed' == 'asyncgen not started'

tests/test_dependency_contextmanager.py:217: AssertionError

pytest honors CLI order, so naming the two tests in reversed order is enough to trigger it. Running the whole file in reverse definition order fails 4 of 22 tests (test_async_state, test_sync_state, test_async_raise_other, test_sync_raise_other), and random shuffles (for example under pytest-randomly) fail 2 to 4 tests depending on the seed.

This PR resets the shared state between tests with an autouse fixture, the same pattern tests/test_dependency_yield_except_httpexception.py already uses for the same reason. The file now passes in definition order, reversed, and shuffled. The reset cannot change what the existing tests check: every errors assertion is a membership check after the test's own request, and every state assertion reads keys the test itself transitions.

Tests only, no library changes.

@codspeed

codspeed Bot commented Sep 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing meagoodboy:fix/order-dependent-contextmanager-tests (d7ab49e) with master (50113da)

Open in CodSpeed

@meagoodboy
meagoodboy marked this pull request as ready for review September 16, 2026 11:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant