1# Testing Dependencies with Overrides
2
3## Overriding dependencies during testing
4
5There are some scenarios where you might want to override a dependency during testing.
6
7You don't want the original dependency to run (nor any of the sub-dependencies it might have).
8
9Instead, you want to provide a different dependency that will be used only during tests (possibly only some specific tests), and will provide a value that can be used where the value of the original dependency was used.
10
11### Use cases: external service
12
13An example could be that you have an external authentication provider that you need to call.
14
15You send it a token and it returns an authenticated user.
16
17This provider might be charging you per request, and calling it might take some extra time than if you had a fixed mock user for tests.
18
19You probably want to test the external provider once, but not necessarily call it for every test that runs.
20
21In this case, you can override the dependency that calls that provider, and use a custom dependency that returns a mock user, only for your tests.
22
23### Use the `app.dependency_overrides` attribute
24
25For these cases, your **FastAPI** application has an attribute `app.dependency_overrides`, it is a simple `dict`.
26
27To override a dependency for testing, you put as a key the original dependency (a function), and as the value, your dependency override (another function).
28
29And then **FastAPI** will call that override instead of the original dependency.
30
31```Python hl_lines="26-27  30"
32{!../../../docs_src/dependency_testing/tutorial001.py!}
33```
34
35!!! tip
36    You can set a dependency override for a dependency used anywhere in your **FastAPI** application.
37
38    The original dependency could be used in a *path operation function*, a *path operation decorator* (when you don't use the return value), a `.include_router()` call, etc.
39
40    FastAPI will still be able to override it.
41
42Then you can reset your overrides (remove them) by setting `app.dependency_overrides` to be an empty `dict`:
43
44```Python
45app.dependency_overrides = {}
46```
47
48!!! tip
49    If you want to override a dependency only during some tests, you can set the override at the beginning of the test (inside the test function) and reset it at the end (at the end of the test function).
50