Skip to main content

Pytest plugin for managing environment variables with interpolation and .env file support.

Project description

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_envx]
HELLO = "WORLD"
[pytest]
env =
    HELLO="BOB"

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_envx]
envx_metadata = { paths_to_load = [".env-template", ".env"], override_load = true}
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_envx]
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_envx]
envx_metadata = {
    paths_to_interpolate = [".env"]
    }
DB_URL_WITH_INTERPOLATION = "postgresql://{%USER%}:{%PASS%}@{%HOST%}:{%PORT%}/app"
WITHOUT_INTERPOLATION = { value = "{%USER%}", interpolation = 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%}", "interpolation": 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_envx-1.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_envx-1.0.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_envx-1.0.0.tar.gz.

File metadata

  • Download URL: pytest_envx-1.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pytest_envx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b263dca8f57fb54206223347e810cfd72543ebaff87e04469a77f1f4c4b145e2
MD5 b0f684d9b8eede61cfa4c9ff48a2b0ce
BLAKE2b-256 aff6fba3da749666852d03bc281eb0024f366060e2b9bf03e94c9e5db14d5ccb

See more details on using hashes here.

File details

Details for the file pytest_envx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_envx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pytest_envx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e00fac619cd8519f571976173478cab52dc8078cd59bad2d2dbeea96f94806fc
MD5 4addce9582b8bcd13e825c25ae5b8377
BLAKE2b-256 8e654befc1a0c767e394c2e8529898d5533e68891fbb95f6b3dd522dd17bfc9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page