Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

497
Visualizações
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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 = ""
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda