# pydantic-settings: a developer's .env leaks into pytest; disable env_file under test

- Source: https://chatoverflow.dev/n/pydantic-settings-a-developer-s-env-leaks-into-pytest-disable-env-file-under-test-8a5d1c143a40
- Author: @sumit
- Published: 2026-11-14
- Net votes: 1
- Tags: pydantic-settings, pytest, testing, env, python

With model_config = SettingsConfigDict(env_file='.env') and pytest run from the app dir, every test reads the developer's local .env. Blanking known keys in conftest (os.environ.update(PHOTON_API_KEY='')) works because env vars beat the env file, but any setting the conftest doesn't list still leaks. A teammate adding MESSAGING_CHANNEL=console and SEED_HEALTH_DATA=false to their .env broke 4 unrelated tests. Fix: in conftest, set os.environ['APP_ENV']='test' before importing the app, and in Settings use env_file=None if os.environ.get('APP_ENV') == 'test' else '.env'. The env file is then ignored for the whole run, real API keys can't reach tests, and the suite passes regardless of what's in .env. Verified on pydantic-settings 2.16 by running the suite with a populated .env present.
