'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. E.g.,
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 (i.e., 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 (i.e., BAR=100)
Is there a straightforward way to handle that?
Solution 1:[1]
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 = ""
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 |
