[QUESTION] How to get dependency_overrides to work
See original GitHub issueI’m not sure whether I’m just doing something wrong or if this is a bug. I’m on version 0.42.0
. I’m trying to use dependency_overrides
to override a function for testing, but I can’t get it to work. Here’s a basic example.
routers/some_router.py
from typing import List
from fastapi import APIRouter
router = APIRouter()
def some_func():
return ['foo']
@router.get('/some-endpoint', response_model=List[str])
def some_endpoint() -> List[str]:
return some_func()
test_some_router.py
from fastapi import FastAPI
from starlette.testclient import TestClient
from routers import some_router
app = FastAPI()
app.include_router(some_router.router)
client = TestClient(app)
def some_func_new():
return ['foo', 'bar', 'baz', 'qux']
app.dependency_overrides[some_router.some_func] = some_func_new
def test_some_endpoint():
response = client.get('/some-endpoint')
assert response.status_code == 200
assert response.json() == ['foo', 'bar', 'baz', 'qux']
And the result:
===================== FAILURES =====================
________________ test_some_endpoint ________________
def test_some_endpoint():
response = client.get('/some-endpoint')
assert response.status_code == 200
> assert response.json() == ['foo', 'bar', 'baz', 'qux']
E AssertionError: assert ['foo'] == ['foo', 'bar', 'baz', 'qux']
I’m not sure if I should be importing things or setting them up in a different way. I’ve also tried from main import app, some_router
as well as directly importing some_func
. If I’m doing something wrong, I think the documentation could be made clearer (e.g. a two file example instead of having both the API and tests in the same file).
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
SBT dependencyOverrides in a scope doesn't seem to work ...
I would like to have a different version of library in test scope. I was hoping the simplified version of project descriptor could...
Read more >[QUESTION] Dependency overrides not work in my case #2166
depends_demo.zip I am getting an error when using dependency_overrides https://fastapi.tiangolo.com/advanced/testing-dependencies/ I have ...
Read more >Dependency Overrides - Finaid
A student cannot become independent just because the parents are unwilling to help pay for the student's college education. Although these circumstances are...
Read more >Dependency Overrides - Cappex.com
College financial aid administrators can change a student's dependency status from dependent to independent through a dependency override on a case-by-case ...
Read more >Filling Out the FAFSA: Dependency Override - NerdWallet
Filing for a dependency overrides consists of a lengthy process that ... to any of these questions, you may need to apply for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the end, for the context of my project, we decided to go with not touching anything else but the client fixture, and not use
dependency_overrides
, at least in the mean time:This does the trick for us, hopefully it can also help others!
Thanks @Kludex for going deeper into this, I appreciate it! It’d be indeed good to understand whether this is a limitation of FastAPI or if there’s a possible way around it. In the mean time, I will check whether I can refactor my function to not take any parameters