python
config_error
ai_generated
true
AssertionError: Dependency overrides not applied: response still uses production DB
ID: python/fastapi-dependency-override-not-applied
80%Fix Rate
86%Confidence
0Evidence
2024-10-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 0.100.x | active | — | — | — |
| 0.110.x | active | — | — | — |
Root Cause
Using `app.dependency_overrides` on a different app instance than the one under test, or not clearing overrides after.
generic中文
在测试中使用的app实例与dependency_overrides设置的实例不同,或者未在测试后清理覆盖。
Workarounds
-
95% success
client = TestClient(app)\napp.dependency_overrides[get_db] = fake_db
-
93% success
@pytest.fixture\ndef client():\n yield TestClient(app)\n app.dependency_overrides.clear()
Dead Ends
Common approaches that don't work:
-
70% fail
If the TestClient uses a different app, overrides never apply.
-
80% fail
FastAPI resolves via dependency_overrides, not module lookup.