When to Use
Use when creating new integration tests for FastAPI endpoints within the T1D Companion codebase to ensure consistency and proper setup of the authentication and service-coordinator stack.
Procedure
- Mount Target Router: Ensure the target router is mounted on a fresh
FastAPI()instance. - Dependency Overrides: Use
app.dependency_overridesto inject patched versions ofget_db,require_active_user, andget_current_user. - Mock Coordinator: Attach a mock coordinator instance to
app.state.coordinatorto bypass production service chains. - Test Client: Use
Starlette.testclient.TestClientfor synchronous HTTP calls. - Async/Sync Verification: Mark test functions with
@pytest.mark.asynciofor any async database or service logic assertions. - Teardown: Always include a fixture or finalizer to clear
app.dependency_overridesafter each test to prevent state pollution.
Pitfalls
- Pollution: Not resetting
app.dependency_overridesmakes tests flaky and dependent on execution order. - Initialization: Omitting the mock coordinator in
app.statealmost always causes runtime errors in the Service layer.
Verification
Refer to tests/test_chat_integration.py and tests/test_api_auth.py for canonical implementations.
Source: ruskibeats/t1d — distributed by TomeVault.