You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
10
11
11
## Installation
12
12
@@ -20,19 +20,17 @@ pip install pytest-env
20
20
21
21
### Native form in `pyproject.toml`, `pytest.toml` and `.pytest.toml`
22
22
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
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:
49
48
50
49
- if an inline table you can set options via the `transform` or `skip_if_set` keys, while the `value` key holds the
51
50
value to set (or transform before setting). For transformation the variables you can use is other environment
@@ -67,16 +66,23 @@ env =
67
66
Or with `pyproject.toml`:
68
67
69
68
```toml
70
-
[tool.pytest.ini_options]
69
+
[tool.pytest]
71
70
env = [
72
-
"HOME=~/tmp",
73
-
"RUN_ENV=test",
71
+
"HOME=~/tmp",
72
+
"RUN_ENV=test",
74
73
]
75
74
```
76
75
77
76
### Only set if not already set
78
77
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
+
```
80
86
81
87
```ini
82
88
[pytest]
@@ -87,17 +93,28 @@ env =
87
93
88
94
### Transformation
89
95
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
+
```
92
103
93
104
```ini
94
105
[pytest]
95
106
env =
96
107
RUN_PATH=/run/path/{USER}
97
108
```
98
109
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 }
0 commit comments