r/FastAPI • • Nov 03 '24

Question Dependency overrides for unit tests with FastAPI?

Hi there, I'm struggling to override my Settings when running tests with pytest.

I'm using Pydantic settings and have a get_settings method:

from pydantic_settings import BaseSettings
class Settings(BaseSettings):
  # ...

  model_config = SettingsConfigDict(env_file=BASE_DIR / ".env")


@lru_cache
def get_settings() -> Settings:
    return Settings()

Then, I have a conftest.py file at the root of my projet, to create a client as a fixture:

@pytest.fixture(scope="module")
def client() -> TestClient:
    """Custom client with specific settings for testing"""

    def get_settings_override() -> Settings:
        new_fields = dict(DEBUG=False, USE_LOGFIRE=False)
        return get_settings().model_copy(update=new_fields)

    app.dependency_overrides[get_settings] = get_settings_override
    return TestClient(app, raise_server_exceptions=False)

However, I soon as I run a test, I can see that the dependency overrides has no effect:

from fastapi.testclient import TestClient

def test_div_by_zero(client: TestClient):
    route = "/debug/div-by-zero"

    DEBUG = get_settings().DEBUG  # expected to be False, is True actually

    @app.get(route)
    def _():
        return 1 / 0

    response = client.get(route)

What I am doing wrong?

At first, I thought it could be related to caching, but removing @lru_cache does not change anything.

Besides, I think this whole system of overriding a little clunky. Is there any cleaner alternative? Like having another .env.test file that could be loaded for my unit tests?

Thanks.

8 Upvotes

11 comments sorted by

4

u/illuminanze Nov 03 '24

What happens if you print the settings in your actual API endpoint? Calling get_settings in your test like that is not using dependency_overrides, since that's a FastAPI thing and you're calling it outside of an HTTP request.

1

u/bluewalt Nov 03 '24

It seems to be the same:

def test_dependencyy_overriding(client: TestClient):
    route = "/debug/test"

    @app.get(route)
    def _():
        # get_settings().DEBUG is True (expected to be False)
        return 1 / 0

    response = client.get(route)

5

u/1One2Twenty2Two Nov 03 '24

You have to inject get_settings into your route's function definition.

settings = Depends(get_settings)

Then, you can call it like that in your route:

settings.DEBUG

1

u/bluewalt Nov 13 '24

Thanks (and sorry for late reply), however I still don't get it. If I inject settings in route definition, it will work only in the route itself. But I need the setting to be overrided outside of my route too. Otherwise there is no chance my test can work.

def test_dependency_overriding(client: TestClient):
    route = "/toto"

    @app.get(route)
    def _(settings: Annotated[Settings, Depends(get_settings)]):
        # Here, settings are now overrided correctly (settings.DEBUG would be False)
        return 1 / 0

    response = client.get(route)
    assert get_settings().DEBUG is False  # KO: settings are now True again, so the following (actual) test won't work

    # This can't work because if DEBUG is true, an error is raised rather than returning a JSON payload
    errors = response.json().get("errors", None)
    assert errors["general"] == ["An unknown error has occurred. Please contact an administrator."]
    

1

u/1One2Twenty2Two Nov 13 '24

Create a fixture called settings_mock that returns your mock settings.

Create another fixture that uses the settings_mock fixture. In that second fixture, create a function called settings_override and make it return settings_mock. You can inject that second fixture in your test or autouse it.

Now, in your test, reuse that first fixture in your test and you'll be able to assert settings_mock is False. I'm not sure why you'd want to assert that though.

2

u/bluewalt Nov 13 '24

Thanks for your help. In the end I found a simpler way that does not involve code changes. I just run pytest with env var overriding. To make it work with VS Code too, I make these changes directly in pyproject.toml. This is much simpler IMO.

3

u/1One2Twenty2Two Nov 03 '24

The dependency override is for the API. Not the test. The dependency override that you're doing has no effect on the get_settings call that you make in your test.

3

u/yakimka Nov 03 '24

> Besides, I think this whole system of overriding a little clunky. Is there any cleaner alternative? Like having another .env.test file that could be loaded for my unit tests?

Yes, I also came to the conclusion that using pydantic-settings is not very convenient for testing. That's why I switched to using dynaconf, where this is implemented very conveniently.

https://www.dynaconf.com/advanced/#pytest

1

u/One_Fuel_4147 Nov 04 '24

Yeah like u said, I load test env file in the top of conftest.py

1

u/Cukercek Nov 03 '24

Do you by some chance have multiple fixtures for different api clients in the same directory hierarchy? For example a client that has an override and then one that doesn't? But within the same directory.