https://github.com/pytest-dev/pytest-env
pytest plugin to set environment variables in pytest.ini or pyproject.toml file
https://github.com/pytest-dev/pytest-env
plugin pytest python
Last synced: 25 days ago
JSON representation
pytest plugin to set environment variables in pytest.ini or pyproject.toml file
- Host: GitHub
- URL: https://github.com/pytest-dev/pytest-env
- Owner: pytest-dev
- License: mit
- Created: 2022-10-10T19:18:13.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T22:40:07.000Z (7 months ago)
- Last Synced: 2024-10-30T08:03:40.841Z (7 months ago)
- Topics: plugin, pytest, python
- Language: Python
- Homepage: https://pypi.org/project/pytest-env/
- Size: 114 KB
- Stars: 147
- Watchers: 8
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
- awesome-pyproject - pytest-env - A pytest plugin that enables you to set environment variables in a pyproject.toml file. (Testing)
README
# pytest-env
[](https://pypi.org/project/pytest-env/)
[](https://pypi.org/project/pytest-env/)
[](https://github.com/pytest-dev/pytest-env/actions/workflows/check.yaml)
[](https://pepy.tech/project/pytest-env)This is a `pytest` plugin that enables you to set environment variables in a `pytest.ini` or `pyproject.toml` file.
## Installation
Install with pip:
```shell
pip install pytest-env
```## Usage
### Native form in `pyproject.toml`
```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:```ini
[pytest]
env =
HOME=~/tmp
RUN_ENV=test
```Or with `pyproject.toml`:
```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:
```ini
[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:```ini
[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):```ini
[pytest]
env =
R:RUN_PATH=/run/path/{USER}
R:D:RUN_PATH_IF_NOT_SET=/run/path/{USER}
```