'FastAPI: app.dependency_overrides affects other test files

I'm working with FastAPI. I've introduced in my test suite (pytest) app.dependency_overrides to test my app dependencies. Strangely enough, when I use it in a test file, tests in other files start failing. It looks like app.dependency_overrides affects what happens in other tests.

Example:

tests
├── test_1.py
└── test_2.py

test_1.py

def foo():
    return "foo"

client = TestClient(app)
 
app.dependency_overrides[get_settings] = foo

def test_1():
    response = client.get("/ping")
    assert response.status_code == 200
    assert response.json() == {"ping": "pong"}

test_2.py

client = TestClient(app)

def test_2():
    print(app.dependency_overrides)
    response = client.get("/ping")
    assert response.status_code == 200
    assert response.json() == {"ping": "pong"}

Output

tests/test_1.py::test_1 PASSED
tests/test_2.py::test_2 {<functools._lru_cache_wrapper object at 0x10f513cc0>: <function foo at 0x10ea34a60>}
PASSED

As you can see, dependencies overridden in test_1.py are affecting test_2.py. The doc states:

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).

I was wondering that such a rule applies intra-module (i.e., each test file starts with an empty app.dependency_overrides), but not inter-module. Looks like I was wrong.

Is there a way to isolate the effect of app.dependency_overrides to the all the tests in each file without affecting other modules of the test suite? (Apart from defining custom app.dependency_overrides in each(!) test function)



Solution 1:[1]

In your code, the statement app.dependency_overrides[get_settings] = foo effects all the tests which are executed after it is evaluated, since there is no cleanup.

To solve this issue and have a simpler way of writing tests which change FastAPI dependencies, I've created the pytest-fastapi-deps library.

Use it like so:

def test_1(fastapi_dep):
    with fastapi_dep(app).override({get_settings: foo}):
        response = client.get("/ping")
        assert response.status_code == 200
        assert response.json() == {"ping": "pong"}

Solution 2:[2]

This works for me:


@pytest.fixture(scope="module")
def admin_user():
    """This mocks the oauth authentication in that it returns a value user
    instead of Depending on the actual implementation of oauthRequired
    """
    app.dependency_overrides[oauthRequired] = lambda: OauthUser(
        token="", profile=__MOCKED_USER
    )
    yield app
    del app.dependency_overrides[oauthRequired]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter K
Solution 2 Fabian Schuh