Skip to content

Speed up FastAPI tests 7x by session-scoping the test client fixture#12820

Merged
RayBB merged 1 commit into
masterfrom
speedup-fastapi-tests
May 28, 2026
Merged

Speed up FastAPI tests 7x by session-scoping the test client fixture#12820
RayBB merged 1 commit into
masterfrom
speedup-fastapi-tests

Conversation

@RayBB

@RayBB RayBB commented May 28, 2026

Copy link
Copy Markdown
Collaborator

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 the fastapi_client pytest 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:

Test group Tests Time Per test Relative speed
Non-FastAPI tests 1,179 1.75s ~1.5ms
FastAPI tests (before) 210 7.50s ~35.7ms ~24× slower

The fastapi_client fixture alone cost 0.04–0.12s per invocation, and ~113 tests (54%) depended on it either directly or through mock_authenticated_user or the client alias.

Solution

Changed the fixture scope from function (default) to session:

# Before
@pytest.fixture
def fastapi_client():
    ...

# After
@pytest.fixture(scope="session")
def fastapi_client():
    ...

The FastAPI app is stateless — it's purely route registration + middleware setup. All mutable state (dependency_overrides in mock_authenticated_user) is properly managed with setup/teardown and is safe because tests run sequentially.

Results

Metric Before After Improvement
FastAPI test suite 7.50s 1.05s 7.1× faster
Tests passing 210/210 210/210 No regressions

Safety analysis

  • dependency_overrides: The mock_authenticated_user fixture sets overrides in setup and calls .clear() in teardown. Since tests run sequentially, no leakage occurs between tests.
  • patch scope: The patch("openlibrary.asgi_app.set_context_from_fastapi") now spans the entire session, but the three test files that don't use fastapi_client (test_models.py, test_auth.py, test_list_view_json.py) create their own standalone FastAPI() instances that never reference this function, so they're unaffected.
  • Cleanup: client.close() is called at session teardown via the existing try/finally block.

Testing

# FastAPI tests pass
uv run --with-requirements requirements_test.txt \
  pytest openlibrary/tests/fastapi/ -v
# → 210 passed

# Benchmark
uv run --with-requirements requirements_test.txt \
  pytest openlibrary/tests/fastapi/ -q
# → 1.05s (down from 7.50s)

@RayBB RayBB changed the title speedup fastapi tests by scoping session Speed up FastAPI tests 7x by session-scoping the test client fixture May 28, 2026
@mekarpeles mekarpeles requested a review from Copilot May 28, 2026 22:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the fastapi_client fixture and update its docstring.

@Sanket17052006 Sanket17052006 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@RayBB

RayBB commented May 28, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for reviewing!

@RayBB RayBB merged commit f40f468 into master May 28, 2026
9 checks passed
@RayBB RayBB deleted the speedup-fastapi-tests branch May 28, 2026 23:37
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.

4 participants