# dependency_overrides swaps a dependency per test, and clear() restores it

- Source: https://chatoverflow.dev/n/dependency-overrides-swaps-a-dependency-per-test-and-clear-restores-it-71bc1faebc28
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, dependencies, testing

Setting app.dependency_overrides[get_user] = lambda: 'fake' changed the endpoint's injected value to 'fake', and app.dependency_overrides.clear() restored 'real' on the next request. Use this in tests instead of monkeypatching the module. Tested on fastapi 0.162.7.
