Skip to main content

logo

PyPI version Python versions codecov License

🌍 Select Language

🔧 pytest-envx

pytest-envx is a powerful and user-friendly plugin for pytest, allowing you to easily manage environment variables from configuration files like pyproject.toml and pytest.ini.


🚀 Features

  • ✔️ Environment variable interpolation via templates
  • ✔️ Load variables from .env files
  • ✔️ Compatible with both pyproject.toml and pytest.ini
  • ✔️ Easy to configure and use
  • ✔️ Flexible support for overriding and loading order

⚡️ Installation

pip install pytest-envx

📦 Quick Start

Create a configuration file — either pyproject.toml ⚙️ or pytest.ini ⚙️ By default, pytest prioritizes pytest.ini ⚙️

✅ Example 1: Simple Variable

pyproject.toml ⚙️ pytest.ini ⚙️
[tool.pytest.ini_options]
env = [
    "HELLO='WORLD'"
]
[pytest]
env =
    HELLO="WORLD"

test_example.py 🐍:

import os

def test_env_var():
    assert os.getenv("HELLO") == "WORLD"

✅ Example 2: Load from .env 🔐 files with override

.env-template 🔐 .env 🔐
NAME=ALICE
LASTNAME=BAILER
      
NAME=BOB
      
pyproject.toml ⚙️ pytest.ini ⚙️
[tool.pytest.ini_options]
envx_metadata = { paths_to_load = [".env-template", ".env"], override_load = true }
env = [
    "GREETING='Hello'"
]
      
[pytest]
envx_metadata = {"paths_to_load": [".env-template", ".env"], "override_load": True}
env =
    GREETING="Hello"
      

test_env_load.py 🐍:

import os

def test_env_loading():
    assert os.getenv("NAME") == "BOB"
    assert os.getenv("LASTNAME") == "BAILER"
    assert os.getenv("GREETING") == "Hello"

✅ Example 3: Load from .env 🔐 files without override

.env.default 🔐 .env.dev 🔐
MODE=default
      
MODE=development
LEVEL=DEV
      
pyproject.toml ⚙️ pytest.ini ⚙️
[tool.pytest.ini_options]
envx_metadata = { paths_to_load = [".env.default", ".env.dev"], override_load = false }
      
[pytest]
envx_metadata = {"paths_to_load": [".env.default", ".env.dev"], "override_load": False}
      

test_priority.py 🐍:

import os

def test_env_priority():
    assert os.getenv("MODE") == "default"
    assert os.getenv("LEVEL") == "DEV"

✅ Example 4: Variable Interpolation

.env 🔐
USER=john
PASS=secret
HOST=db.local
PORT=5432
      
pyproject.toml ⚙️ pytest.ini ⚙️
[tool.pytest.ini_options]
envx_metadata = { paths_to_interpolate = [".env"] }
env = [
    "DB_URL_WITH_INTERPOLATION='postgresql://{%USER%}:{%PASS%}@{%HOST%}:{%PORT%}/app'",
    "WITHOUT_INTERPOLATION={'value': '{%USER%}', 'interpolate': False}",
    "NOT_FOUND='{%NOT_FOUND%}'"
]
      
[pytest]
envx_metadata = {"paths_to_interpolate": [".env"]}
env =
    DB_URL_WITH_INTERPOLATION="postgresql://{%USER%}:{%PASS%}@{%HOST%}:{%PORT%}/app"
    WITHOUT_INTERPOLATION={"value": "{%USER%}", "interpolate": False}
    NOT_FOUND = "{%NOT_FOUND%}"
      

test_interpolation.py 🐍:

import os

def test_interpolated_value():
    assert os.getenv("DB_URL_WITH_INTERPOLATION") == "postgresql://john:secret@db.local:5432/app"
    assert os.getenv("WITHOUT_INTERPOLATION") == "{%USER%}"
    assert os.getenv("NOT_FOUND") == "{%NOT_FOUND%}"
    assert os.getenv("USER") !=  "john"
    assert os.getenv("PASS") !=  "secret"
    assert os.getenv("HOST") !=  "db.local"
    assert os.getenv("PORT") !=  "5432"

⚙️ envx_metadata

Parameter Type Description
paths_to_load list Paths to .env files to load environment variables from (loaded in order)
override_load (bool, default=True) Whether to override existing environment variables during load
paths_to_interpolate list Paths to files for value interpolation
override_interpolate (bool, default=True) Whether to override variables during interpolation

📄 License

MIT

Metadata

Release files for pytest-envx 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-envx 2.0.0
File Size Uploaded
pytest_envx-2.0.0.tar.gz 13.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-envx 2.0.0
File Interpreter ABI Platform
pytest_envx-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.3 kB

Release files / pytest_envx-2.0.0.tar.gz

Download URL pytest_envx-2.0.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bbccf3c1a2c375ff6810613a0f24767eabf93334e0577c578f06194638fec842
BLAKE2b-256 checksum
How to use checksums
d3128d1a4bd4b12af896c709f2ce11436158a64616e4f96ec4cbc3495326a5a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / pytest_envx-2.0.0-py3-none-any.whl

Download URL pytest_envx-2.0.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ae0fcc2a6c9870a5ab51fc56e628f0f9c5b22e9beae28f8ad957eee6da23a11
BLAKE2b-256 checksum
How to use checksums
d8c8fa2ad8d78f71aa3b7773302102f4997f5cbebc4c0c211894b937cadd7503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page