question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to get dependency_overrides to work

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
larissaleitecommented, Oct 11, 2020

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:

@pytest.fixture
def client(mocker):
    mocker.patch("app.routers.my_router.permission_required", return_value=lambda: None)
    app = FastAPI()
    app.include_router(my_router.router())
    client = TestClient(app)
    yield client

This does the trick for us, hopefully it can also help others!

2reactions
larissaleitecommented, Oct 10, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found