python config_error ai_generated true

AssertionError: Dependency overrides not applied: response still uses production DB

ID: python/fastapi-dependency-override-not-applied

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-10-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    client = TestClient(app)\napp.dependency_overrides[get_db] = fake_db
  2. 93% success
    @pytest.fixture\ndef client():\n    yield TestClient(app)\n    app.dependency_overrides.clear()

Dead Ends

Common approaches that don't work:

  1. 70% fail

    If the TestClient uses a different app, overrides never apply.

  2. 80% fail

    FastAPI resolves via dependency_overrides, not module lookup.