简体   繁体   中英

Pydantic settings management + FastAPI: how to ignore a .env file during tests with pytest?

I'm using Pydantic settings management in a FastAPI-based project. I have a Settings class like this one:

class Settings(BaseSettings):
    FOO: str = ''
    BAR: int = 0

    class Config:
        env_file = "path/to/.my_env_file")
        env_nested_delimiter = "__"

The file path/to/.my_env_file contains FOO and BAR values.

During tests, I need to selectively patch Settings , and I do not want to read anything from path/to/.my_env_file . Eg,

path/to/.my_env_file

FOO=i_do_not_wanna_read_this
BAR=100

my test file:

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

def get_settings_override() -> Settings:
    return Settings(
        FOO = 'foo'
    )

app.dependency_overrides[get_settings] = get_settings_override

I want to run tests with FOO='foo' and with the default value of BAR (ie, BAR=0 , ignoring the content of path/to/.my_env_file . In the code above, I get FOO='foo' but BAR is still read from path/to/.my_env_file (ie, BAR=100 )

Is there a straightforward way to handle that?

While I couldn't find a straightforward solution in the docs, or any other page, this worked for my tests:

When using tox, put this into your tox.ini , as per this Stack Overflow question :

[testenv]
setenv = TOX_TESTENV = true

You can then simply use the following snippet to override your env_file settings:

import os

# ... snip ...

if os.environ.get("TOX_TESTENV") is not None:
    Settings.Config.env_file = ""

Similar approaches, for example by checking sys.argv for the existence of "test" or checking if unittest is loaded should work fine too:

import sys

# ... snip ...

if len(sys.argv) > 1 and "pytest" in sys.argv[0]:
    Settings.Config.env_file = ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM