pytest-env 1.2.0


pip install pytest-env

  Latest version

Released: Oct 09, 2025


Meta
Maintainer: Bernát Gábor
Requires Python: >=3.10

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

License
  • OSI Approved :: MIT License

Operating System
  • OS Independent

Programming Language
  • Python
  • Python :: 3 :: Only
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: Implementation :: CPython

Topic
  • Software Development :: Libraries :: Python Modules

pytest-env

PyPI Supported Python versions check Downloads

This is a pytest plugin that enables you to set environment variables in a pytest.ini or pyproject.toml file.

Installation

Install with pip:

pip install pytest-env

Usage

Native form in pyproject.toml

[tool.pytest_env]
HOME = "~/tmp"
RUN_ENV = 1
TRANSFORMED = {value = "{USER}/alpha", transform = true}
SKIP_IF_SET = {value = "on", skip_if_set = true}

The tool.pytest_env tables keys are the environment variables keys to set. The right hand side of the assignment:

  • if an inline table you can set options via the transform or skip_if_set keys, while the value key holds the value to set (or transform before setting). For transformation the variables you can use is other environment variable,
  • otherwise the value to set for the environment variable to set (casted to a string).

Via pytest configurations

In your pytest.ini file add a key value pair with env as the key and the environment variables as a line separated list of KEY=VALUE entries. The defined variables will be added to the environment before any tests are run:

[pytest]
env =
    HOME=~/tmp
    RUN_ENV=test

Or with pyproject.toml:

[tool.pytest.ini_options]
env = [
    "HOME=~/tmp",
    "RUN_ENV=test",
]

Only set if not already set

You can use D: (default) as prefix if you don't want to override existing environment variables:

[pytest]
env =
    D:HOME=~/tmp
    D:RUN_ENV=test

Transformation

You can use existing environment variables using a python-like format, these environment variables will be expended before setting the environment variable:

[pytest]
env =
    RUN_PATH=/run/path/{USER}

You can apply the R: prefix to keep the raw value and skip this transformation step (can combine with the D: flag, order is not important):

[pytest]
env =
    R:RUN_PATH=/run/path/{USER}
    R:D:RUN_PATH_IF_NOT_SET=/run/path/{USER}

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
pytest (>=8.4.2)
tomli (>=2.2.1)