Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

412
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!