{"id":2233,"library":"pytest-dotenv","title":"pytest-dotenv","description":"pytest-dotenv is a Pytest plugin that facilitates the loading of environment variables from `.env` files before running tests. It integrates seamlessly with Pytest's plugin system, automatically detecting and parsing `.env` files in your project. The current version is 0.5.2, with its last release in June 2020, indicating a stable but infrequently updated project.","status":"maintenance","version":"0.5.2","language":"en","source_language":"en","source_url":"https://github.com/quiqua/pytest-dotenv","tags":["pytest","dotenv","testing","environment variables","configuration"],"install":[{"cmd":"pip install pytest-dotenv","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core testing framework; this is a plugin for pytest.","package":"pytest","optional":false},{"reason":"Used for parsing and loading .env files.","package":"python-dotenv","optional":false}],"imports":[{"note":"As a pytest plugin, it integrates automatically upon installation. Explicit imports in test files are generally not required for its core functionality.","symbol":"No direct imports for basic usage","correct":"Configuration is primarily done via pytest.ini or command-line options."}],"quickstart":{"code":"# 1. Create a .env file in your project root:\n# .env\nMY_SECRET_KEY=supersecretkey123\nDATABASE_URL=postgresql://user:password@host:port/dbname\n\n# 2. Create a test file, e.g., test_settings.py:\n# test_settings.py\nimport os\n\ndef test_environment_variables_loaded():\n    assert os.getenv('MY_SECRET_KEY') == 'supersecretkey123'\n    assert os.getenv('DATABASE_URL') == 'postgresql://user:password@host:port/dbname'\n\n# 3. (Optional) Create a pytest.ini for custom configuration:\n# pytest.ini\n# [pytest]\n# env_files = .env.test .env\n# env_override_existing_values = 1\n\n# 4. Run pytest from your terminal:\n# pytest","lang":"python","description":"To get started, simply install the plugin and create a `.env` file in your project's root directory. pytest-dotenv will automatically load these variables when tests are run. For advanced usage, you can configure custom `.env` file paths or override existing system environment variables using a `pytest.ini` configuration file."},"warnings":[{"fix":"Add `env_override_existing_values = 1` under the `[pytest]` section in your `pytest.ini` file, or use `pytest --envfile path/to/.env`.","message":"By default, `pytest-dotenv` will not override environment variables that are already defined in the process's environment. If you need to force overriding, you must explicitly enable `env_override_existing_values = 1` in your `pytest.ini` or use the `--envfile` CLI option (which always overrides).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Specify `env_files = .env.test .env.local .env` in your `pytest.ini` to define a custom loading order and paths. Files are loaded in the order listed.","message":"If not explicitly configured, `pytest-dotenv` searches for `.env` files in the working directory and then in ancestor directories. If you have multiple `.env` files or specific naming conventions, ensure you define them using `env_files` in `pytest.ini` to control precedence.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always add `.env` (and any other files containing sensitive data) to your `.gitignore` file to prevent accidental commits.","message":"Accidentally committing `.env` files, which often contain sensitive credentials, to version control systems like Git is a common security risk.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Standardize on a single method for loading environment variables. If using `pytest-dotenv`, rely on its configuration options. If manual loading is preferred, ensure `autouse=True` fixtures are properly scoped and handle overrides as expected.","message":"Be aware of potential conflicts or unexpected behavior if you are also using other pytest plugins that manage environment variables (e.g., `pytest-env`) or if you manually load `python-dotenv` within your `conftest.py` with different `override` settings. The precedence rules between multiple plugins or manual loading can be complex.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}