Skip to content

Commit 79af961

Browse files
authored
✨ feat(config): support [tool.pytest] table in pyproject.toml (#189)
1 parent 4294e39 commit 79af961

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

README.md

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# pytest-env
22

33
[![PyPI](https://img.shields.io/pypi/v/pytest-env?style=flat-square)](https://pypi.org/project/pytest-env/)
4-
[![Supported Python
5-
versions](https://img.shields.io/pypi/pyversions/pytest-env.svg)](https://pypi.org/project/pytest-env/)
4+
[![Supported Python versions](https://img.shields.io/pypi/pyversions/pytest-env.svg)](https://pypi.org/project/pytest-env/)
65
[![check](https://github.com/pytest-dev/pytest-env/actions/workflows/check.yaml/badge.svg)](https://github.com/pytest-dev/pytest-env/actions/workflows/check.yaml)
76
[![Downloads](https://static.pepy.tech/badge/pytest-env/month)](https://pepy.tech/project/pytest-env)
87

9-
This is a `pytest` plugin that enables you to set environment variables in `pytest.ini`, `pyproject.toml`, `pytest.toml` or `.pytest.toml` files.
8+
This is a `pytest` plugin that enables you to set environment variables in `pytest.ini`, `pyproject.toml`, `pytest.toml`
9+
or `.pytest.toml` files.
1010

1111
## Installation
1212

@@ -20,19 +20,17 @@ pip install pytest-env
2020

2121
### Native form in `pyproject.toml`, `pytest.toml` and `.pytest.toml`
2222

23-
> [!NOTE]
24-
> `pytest.toml` and `.pytest.toml` is only supported on Pytest 9.0+.
25-
26-
Native form takes precedence over the `pytest.ini` form. `pytest.toml` takes precedence over `.pytest.toml`, and that takes precedence over `pyproject.toml`.
23+
Native form takes precedence over the `pytest.ini` form. `pytest.toml` takes precedence over `.pytest.toml`, and that
24+
takes precedence over `pyproject.toml`.
2725

2826
In `pyproject.toml`:
2927

3028
```toml
3129
[tool.pytest_env]
3230
HOME = "~/tmp"
3331
RUN_ENV = 1
34-
TRANSFORMED = {value = "{USER}/alpha", transform = true}
35-
SKIP_IF_SET = {value = "on", skip_if_set = true}
32+
TRANSFORMED = { value = "{USER}/alpha", transform = true }
33+
SKIP_IF_SET = { value = "on", skip_if_set = true }
3634
```
3735

3836
In `pytest.toml` (or `.pytest.toml`):
@@ -41,11 +39,12 @@ In `pytest.toml` (or `.pytest.toml`):
4139
[pytest_env]
4240
HOME = "~/tmp"
4341
RUN_ENV = 1
44-
TRANSFORMED = {value = "{USER}/alpha", transform = true}
45-
SKIP_IF_SET = {value = "on", skip_if_set = true}
42+
TRANSFORMED = { value = "{USER}/alpha", transform = true }
43+
SKIP_IF_SET = { value = "on", skip_if_set = true }
4644
```
4745

48-
The `tool.pytest_env` (`pytest_env` in `pytest.toml` and `.pytest.toml`) tables keys are the environment variables keys to set. The right hand side of the assignment:
46+
The `tool.pytest_env` (`pytest_env` in `pytest.toml` and `.pytest.toml`) tables keys are the environment variables keys
47+
to set. The right hand side of the assignment:
4948

5049
- if an inline table you can set options via the `transform` or `skip_if_set` keys, while the `value` key holds the
5150
value to set (or transform before setting). For transformation the variables you can use is other environment
@@ -67,16 +66,23 @@ env =
6766
Or with `pyproject.toml`:
6867

6968
```toml
70-
[tool.pytest.ini_options]
69+
[tool.pytest]
7170
env = [
72-
"HOME=~/tmp",
73-
"RUN_ENV=test",
71+
"HOME=~/tmp",
72+
"RUN_ENV=test",
7473
]
7574
```
7675

7776
### Only set if not already set
7877

79-
You can use `D:` (default) as prefix if you don't want to override existing environment variables:
78+
Use `skip_if_set = true` in the native TOML form, or the `D:` (default) prefix in INI form, to only set the variable
79+
when it is not already defined in the environment:
80+
81+
```toml
82+
[pytest_env]
83+
HOME = { value = "~/tmp", skip_if_set = true }
84+
RUN_ENV = { value = "test", skip_if_set = true }
85+
```
8086

8187
```ini
8288
[pytest]
@@ -87,17 +93,28 @@ env =
8793

8894
### Transformation
8995

90-
You can use existing environment variables using a python-like format, these environment variables will be expended
91-
before setting the environment variable:
96+
You can reference existing environment variables using a python-like format. Use `transform = true` in the native TOML
97+
form, or omit the `R:` prefix in INI form (transformation is the default in INI):
98+
99+
```toml
100+
[pytest_env]
101+
RUN_PATH = { value = "/run/path/{USER}", transform = true }
102+
```
92103

93104
```ini
94105
[pytest]
95106
env =
96107
RUN_PATH=/run/path/{USER}
97108
```
98109

99-
You can apply the `R:` prefix to keep the raw value and skip this transformation step (can combine with the `D:` flag,
100-
order is not important):
110+
To keep the raw value and skip transformation, omit `transform` (or set it to `false`) in TOML, or apply the `R:` prefix
111+
in INI (can combine with `D:`/`skip_if_set`, order is not important):
112+
113+
```toml
114+
[pytest_env]
115+
RUN_PATH = { value = "/run/path/{USER}" }
116+
RUN_PATH_IF_NOT_SET = { value = "/run/path/{USER}", skip_if_set = true }
117+
```
101118

102119
```ini
103120
[pytest]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dynamic = [
3636
"version",
3737
]
3838
dependencies = [
39-
"pytest>=8.4.2",
39+
"pytest>=9",
4040
"tomli>=2.2.1; python_version<'3.11'",
4141
]
4242
optional-dependencies.testing = [

tests/test_env.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ def test_env_via_pytest(
139139
None,
140140
id="pyproject toml via ini_options",
141141
),
142+
pytest.param(
143+
{},
144+
'[tool.pytest]\nenv = ["MAGIC=toml", "MAGIC_2=toml2"]',
145+
"",
146+
"",
147+
{"MAGIC": "toml", "MAGIC_2": "toml2"},
148+
None,
149+
id="pyproject toml via tool.pytest",
150+
),
142151
pytest.param(
143152
{},
144153
'[tool.pytest_env]\nMAGIC = 1\nMAGIC_2 = "toml2"',

0 commit comments

Comments
 (0)