Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

421
Vistas
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)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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"}
over 4 years ago · Santiago Trujillo Denunciar

0

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]

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda