This is a pytest plugin that enables you to set environment variables in the pytest.ini file.
Install with pip:
pip install pytest-envIn 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=testOr with pyproject.toml:
[tool.pytest.ini_options]
env = [
"HOME=~/tmp",
"RUN_ENV=test",
]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=testYou 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}