Speed up FastAPI tests 7x by session-scoping the test client fixture#12820
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Changes the fastapi_client pytest fixture from default function scope to session scope so the FastAPI app (with all routers, middlewares, and static mounts) is built only once per test session, reducing the FastAPI test suite runtime from ~7.5s to ~1.05s.
Changes:
- Add
scope="session"to thefastapi_clientfixture and update its docstring.
Sanket17052006
left a comment
Contributor
There was a problem hiding this comment.
Tested locally :
On master branch -
docker compose run --rm home pytest openlibrary/tests/fastapi/ -q
210 passed, 3 warnings in 5.90s
On PR branch -
docker compose run --rm home pytest openlibrary/tests/fastapi/ -q
210 passed, 3 warnings in 0.95s
Collaborator
Author
|
Thanks for reviewing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Speed up FastAPI tests 7x by session-scoping the test client fixture
Problem
The FastAPI test suite (
openlibrary/tests/fastapi/) was significantly slower than it needed to be. Profiling revealed that thefastapi_clientpytest fixture was function-scoped (the default), meaning it recreated the entire FastAPI app from scratch for every single test — including importing and registering all 13 routers, adding middlewares, mounting static files, etc.Benchmarks showed the disparity clearly:
The
fastapi_clientfixture alone cost 0.04–0.12s per invocation, and ~113 tests (54%) depended on it either directly or throughmock_authenticated_useror theclientalias.Solution
Changed the fixture scope from
function(default) tosession:The FastAPI app is stateless — it's purely route registration + middleware setup. All mutable state (
dependency_overridesinmock_authenticated_user) is properly managed with setup/teardown and is safe because tests run sequentially.Results
Safety analysis
dependency_overrides: Themock_authenticated_userfixture sets overrides in setup and calls.clear()in teardown. Since tests run sequentially, no leakage occurs between tests.patchscope: Thepatch("openlibrary.asgi_app.set_context_from_fastapi")now spans the entire session, but the three test files that don't usefastapi_client(test_models.py,test_auth.py,test_list_view_json.py) create their own standaloneFastAPI()instances that never reference this function, so they're unaffected.client.close()is called at session teardown via the existingtry/finallyblock.Testing