Browse Source
* ✨ Implement dependency overrides for testing * ✅ Add docs source tests and extra tests for dependency overrides * 📝 Add docs for testing dependencies with overridespull/292/head
committed by
GitHub
10 changed files with 562 additions and 14 deletions
@ -0,0 +1,55 @@ |
|||
from fastapi import Depends, FastAPI |
|||
from starlette.testclient import TestClient |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
async def common_parameters(q: str = None, skip: int = 0, limit: int = 100): |
|||
return {"q": q, "skip": skip, "limit": limit} |
|||
|
|||
|
|||
@app.get("/items/") |
|||
async def read_items(commons: dict = Depends(common_parameters)): |
|||
return {"message": "Hello Items!", "params": commons} |
|||
|
|||
|
|||
@app.get("/users/") |
|||
async def read_users(commons: dict = Depends(common_parameters)): |
|||
return {"message": "Hello Users!", "params": commons} |
|||
|
|||
|
|||
client = TestClient(app) |
|||
|
|||
|
|||
async def override_dependency(q: str = None): |
|||
return {"q": q, "skip": 5, "limit": 10} |
|||
|
|||
|
|||
app.dependency_overrides[common_parameters] = override_dependency |
|||
|
|||
|
|||
def test_override_in_items(): |
|||
response = client.get("/items/") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Items!", |
|||
"params": {"q": None, "skip": 5, "limit": 10}, |
|||
} |
|||
|
|||
|
|||
def test_override_in_items_with_q(): |
|||
response = client.get("/items/?q=foo") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Items!", |
|||
"params": {"q": "foo", "skip": 5, "limit": 10}, |
|||
} |
|||
|
|||
|
|||
def test_override_in_items_with_params(): |
|||
response = client.get("/items/?q=foo&skip=100&limit=200") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Items!", |
|||
"params": {"q": "foo", "skip": 5, "limit": 10}, |
|||
} |
@ -0,0 +1,59 @@ |
|||
## Overriding dependencies during testing |
|||
|
|||
There are some scenarios where you might want to override a dependency during testing. |
|||
|
|||
You don't want the original dependency to run (nor any of the sub-dependencies it might have). |
|||
|
|||
Instead, 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. |
|||
|
|||
### Use cases: external service |
|||
|
|||
An example could be that you have an external authentication provider that you need to call. |
|||
|
|||
You send it a token and it returns an authenticated user. |
|||
|
|||
This 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. |
|||
|
|||
You probably want to test the external provider once, but not necessarily call it for every test that runs. |
|||
|
|||
In 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. |
|||
|
|||
### Use case: testing database |
|||
|
|||
Other example could be that you are using a specific database only for testing. |
|||
|
|||
Your normal dependency would return a database session. |
|||
|
|||
But then, after each test, you could want to rollback all the operations or remove data. |
|||
|
|||
Or you could want to alter the data before the tests run, etc. |
|||
|
|||
In this case, you could use a dependency override to return your *custom* database session instead of the one that would be used normally. |
|||
|
|||
### Use the `app.dependency_overrides` attribute |
|||
|
|||
For these cases, your **FastAPI** application has an attribute `app.dependency_overrides`, it is a simple `dict`. |
|||
|
|||
To override a dependency for testing, you put as a key the original dependency (a function), and as the value, your dependency override (another function). |
|||
|
|||
And then **FastAPI** will call that override instead of the original dependency. |
|||
|
|||
```Python hl_lines="24 25 28" |
|||
{!./src/dependency_testing/tutorial001.py!} |
|||
``` |
|||
|
|||
!!! tip |
|||
You can set a dependency override for a dependency used anywhere in your **FastAPI** application. |
|||
|
|||
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. |
|||
|
|||
FastAPI will still be able to override it. |
|||
|
|||
Then you can reset your overrides (remove them) by setting `app.dependency_overrides` to be an empty `dict`: |
|||
|
|||
```Python |
|||
app.dependency_overrides = {} |
|||
``` |
|||
|
|||
!!! tip |
|||
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). |
@ -0,0 +1,313 @@ |
|||
import pytest |
|||
from fastapi import APIRouter, Depends, FastAPI |
|||
from starlette.testclient import TestClient |
|||
|
|||
app = FastAPI() |
|||
|
|||
router = APIRouter() |
|||
|
|||
|
|||
async def common_parameters(q: str, skip: int = 0, limit: int = 100): |
|||
return {"q": q, "skip": skip, "limit": limit} |
|||
|
|||
|
|||
@app.get("/main-depends/") |
|||
async def main_depends(commons: dict = Depends(common_parameters)): |
|||
return {"in": "main-depends", "params": commons} |
|||
|
|||
|
|||
@app.get("/decorator-depends/", dependencies=[Depends(common_parameters)]) |
|||
async def decorator_depends(): |
|||
return {"in": "decorator-depends"} |
|||
|
|||
|
|||
@router.get("/router-depends/") |
|||
async def router_depends(commons: dict = Depends(common_parameters)): |
|||
return {"in": "router-depends", "params": commons} |
|||
|
|||
|
|||
@router.get("/router-decorator-depends/", dependencies=[Depends(common_parameters)]) |
|||
async def router_decorator_depends(): |
|||
return {"in": "router-decorator-depends"} |
|||
|
|||
|
|||
app.include_router(router) |
|||
|
|||
client = TestClient(app) |
|||
|
|||
|
|||
async def overrider_dependency_simple(q: str = None): |
|||
return {"q": q, "skip": 5, "limit": 10} |
|||
|
|||
|
|||
async def overrider_sub_dependency(k: str): |
|||
return {"k": k} |
|||
|
|||
|
|||
async def overrider_dependency_with_sub(msg: dict = Depends(overrider_sub_dependency)): |
|||
return msg |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"url,status_code,expected", |
|||
[ |
|||
( |
|||
"/main-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "q"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/main-depends/?q=foo", |
|||
200, |
|||
{"in": "main-depends", "params": {"q": "foo", "skip": 0, "limit": 100}}, |
|||
), |
|||
( |
|||
"/main-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "main-depends", "params": {"q": "foo", "skip": 100, "limit": 200}}, |
|||
), |
|||
( |
|||
"/decorator-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "q"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
("/decorator-depends/?q=foo", 200, {"in": "decorator-depends"}), |
|||
( |
|||
"/decorator-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "decorator-depends"}, |
|||
), |
|||
( |
|||
"/router-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "q"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/router-depends/?q=foo", |
|||
200, |
|||
{"in": "router-depends", "params": {"q": "foo", "skip": 0, "limit": 100}}, |
|||
), |
|||
( |
|||
"/router-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "router-depends", "params": {"q": "foo", "skip": 100, "limit": 200}}, |
|||
), |
|||
( |
|||
"/router-decorator-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "q"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
("/router-decorator-depends/?q=foo", 200, {"in": "router-decorator-depends"}), |
|||
( |
|||
"/router-decorator-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "router-decorator-depends"}, |
|||
), |
|||
], |
|||
) |
|||
def test_normal_app(url, status_code, expected): |
|||
response = client.get(url) |
|||
assert response.status_code == status_code |
|||
assert response.json() == expected |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"url,status_code,expected", |
|||
[ |
|||
( |
|||
"/main-depends/", |
|||
200, |
|||
{"in": "main-depends", "params": {"q": None, "skip": 5, "limit": 10}}, |
|||
), |
|||
( |
|||
"/main-depends/?q=foo", |
|||
200, |
|||
{"in": "main-depends", "params": {"q": "foo", "skip": 5, "limit": 10}}, |
|||
), |
|||
( |
|||
"/main-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "main-depends", "params": {"q": "foo", "skip": 5, "limit": 10}}, |
|||
), |
|||
("/decorator-depends/", 200, {"in": "decorator-depends"}), |
|||
( |
|||
"/router-depends/", |
|||
200, |
|||
{"in": "router-depends", "params": {"q": None, "skip": 5, "limit": 10}}, |
|||
), |
|||
( |
|||
"/router-depends/?q=foo", |
|||
200, |
|||
{"in": "router-depends", "params": {"q": "foo", "skip": 5, "limit": 10}}, |
|||
), |
|||
( |
|||
"/router-depends/?q=foo&skip=100&limit=200", |
|||
200, |
|||
{"in": "router-depends", "params": {"q": "foo", "skip": 5, "limit": 10}}, |
|||
), |
|||
("/router-decorator-depends/", 200, {"in": "router-decorator-depends"}), |
|||
], |
|||
) |
|||
def test_override_simple(url, status_code, expected): |
|||
app.dependency_overrides[common_parameters] = overrider_dependency_simple |
|||
response = client.get(url) |
|||
assert response.status_code == status_code |
|||
assert response.json() == expected |
|||
app.dependency_overrides = {} |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"url,status_code,expected", |
|||
[ |
|||
( |
|||
"/main-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/main-depends/?q=foo", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
("/main-depends/?k=bar", 200, {"in": "main-depends", "params": {"k": "bar"}}), |
|||
( |
|||
"/decorator-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/decorator-depends/?q=foo", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
("/decorator-depends/?k=bar", 200, {"in": "decorator-depends"}), |
|||
( |
|||
"/router-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/router-depends/?q=foo", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/router-depends/?k=bar", |
|||
200, |
|||
{"in": "router-depends", "params": {"k": "bar"}}, |
|||
), |
|||
( |
|||
"/router-decorator-depends/", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
( |
|||
"/router-decorator-depends/?q=foo", |
|||
422, |
|||
{ |
|||
"detail": [ |
|||
{ |
|||
"loc": ["query", "k"], |
|||
"msg": "field required", |
|||
"type": "value_error.missing", |
|||
} |
|||
] |
|||
}, |
|||
), |
|||
("/router-decorator-depends/?k=bar", 200, {"in": "router-decorator-depends"}), |
|||
], |
|||
) |
|||
def test_override_with_sub(url, status_code, expected): |
|||
app.dependency_overrides[common_parameters] = overrider_dependency_with_sub |
|||
response = client.get(url) |
|||
assert response.status_code == status_code |
|||
assert response.json() == expected |
|||
app.dependency_overrides = {} |
@ -0,0 +1,56 @@ |
|||
from dependency_testing.tutorial001 import ( |
|||
app, |
|||
client, |
|||
test_override_in_items, |
|||
test_override_in_items_with_params, |
|||
test_override_in_items_with_q, |
|||
) |
|||
|
|||
|
|||
def test_override_in_items_run(): |
|||
test_override_in_items() |
|||
|
|||
|
|||
def test_override_in_items_with_q_run(): |
|||
test_override_in_items_with_q() |
|||
|
|||
|
|||
def test_override_in_items_with_params_run(): |
|||
test_override_in_items_with_params() |
|||
|
|||
|
|||
def test_override_in_users(): |
|||
response = client.get("/users/") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Users!", |
|||
"params": {"q": None, "skip": 5, "limit": 10}, |
|||
} |
|||
|
|||
|
|||
def test_override_in_users_with_q(): |
|||
response = client.get("/users/?q=foo") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Users!", |
|||
"params": {"q": "foo", "skip": 5, "limit": 10}, |
|||
} |
|||
|
|||
|
|||
def test_override_in_users_with_params(): |
|||
response = client.get("/users/?q=foo&skip=100&limit=200") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Users!", |
|||
"params": {"q": "foo", "skip": 5, "limit": 10}, |
|||
} |
|||
|
|||
|
|||
def test_normal_app(): |
|||
app.dependency_overrides = None |
|||
response = client.get("/items/?q=foo&skip=100&limit=200") |
|||
assert response.status_code == 200 |
|||
assert response.json() == { |
|||
"message": "Hello Items!", |
|||
"params": {"q": "foo", "skip": 100, "limit": 200}, |
|||
} |
Loading…
Reference in new issue