pytest-dotenv

0.5.2 · maintenance · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

# 1. Create a .env file in your project root:
# .env
MY_SECRET_KEY=supersecretkey123
DATABASE_URL=postgresql://user:password@host:port/dbname

# 2. Create a test file, e.g., test_settings.py:
# test_settings.py
import os

def test_environment_variables_loaded():
    assert os.getenv('MY_SECRET_KEY') == 'supersecretkey123'
    assert os.getenv('DATABASE_URL') == 'postgresql://user:password@host:port/dbname'

# 3. (Optional) Create a pytest.ini for custom configuration:
# pytest.ini
# [pytest]
# env_files = .env.test .env
# env_override_existing_values = 1

# 4. Run pytest from your terminal:
# pytest

view raw JSON →