Skip to content

Commit 4492a97

Browse files
committed
📝 docs: document U flag and unset option
1 parent 8eda69f commit 4492a97

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

README.md

Lines changed: 29 additions & 15 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,20 @@ 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+.
23+
> [!NOTE] `pytest.toml` and `.pytest.toml` is only supported on Pytest 9.0+.
2524
26-
Native form takes precedence over the `pytest.ini` form. `pytest.toml` takes precedence over `.pytest.toml`, and that takes precedence over `pyproject.toml`.
25+
Native form takes precedence over the `pytest.ini` form. `pytest.toml` takes precedence over `.pytest.toml`, and that
26+
takes precedence over `pyproject.toml`.
2727

2828
In `pyproject.toml`:
2929

3030
```toml
3131
[tool.pytest_env]
3232
HOME = "~/tmp"
3333
RUN_ENV = 1
34-
TRANSFORMED = {value = "{USER}/alpha", transform = true}
35-
SKIP_IF_SET = {value = "on", skip_if_set = true}
34+
TRANSFORMED = { value = "{USER}/alpha", transform = true }
35+
SKIP_IF_SET = { value = "on", skip_if_set = true }
36+
DATABASE_URL = { unset = true }
3637
```
3738

3839
In `pytest.toml` (or `.pytest.toml`):
@@ -41,14 +42,16 @@ In `pytest.toml` (or `.pytest.toml`):
4142
[pytest_env]
4243
HOME = "~/tmp"
4344
RUN_ENV = 1
44-
TRANSFORMED = {value = "{USER}/alpha", transform = true}
45-
SKIP_IF_SET = {value = "on", skip_if_set = true}
45+
TRANSFORMED = { value = "{USER}/alpha", transform = true }
46+
SKIP_IF_SET = { value = "on", skip_if_set = true }
47+
DATABASE_URL = { unset = true }
4648
```
4749

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:
50+
The `tool.pytest_env` (`pytest_env` in `pytest.toml` and `.pytest.toml`) tables keys are the environment variables keys
51+
to set. The right hand side of the assignment:
4952

50-
- if an inline table you can set options via the `transform` or `skip_if_set` keys, while the `value` key holds the
51-
value to set (or transform before setting). For transformation the variables you can use is other environment
53+
- if an inline table you can set options via the `transform`, `skip_if_set` or `unset` keys, while the `value` key holds
54+
the value to set (or transform before setting). For transformation the variables you can use is other environment
5255
variable,
5356
- otherwise the value to set for the environment variable to set (casted to a string).
5457

@@ -69,8 +72,8 @@ Or with `pyproject.toml`:
6972
```toml
7073
[tool.pytest.ini_options]
7174
env = [
72-
"HOME=~/tmp",
73-
"RUN_ENV=test",
75+
"HOME=~/tmp",
76+
"RUN_ENV=test",
7477
]
7578
```
7679

@@ -105,3 +108,14 @@ env =
105108
R:RUN_PATH=/run/path/{USER}
106109
R:D:RUN_PATH_IF_NOT_SET=/run/path/{USER}
107110
```
111+
112+
### Unsetting variables
113+
114+
You can use `U:` (unset) as prefix to remove an environment variable. This differs from setting a variable to an empty
115+
string — the variable will be completely removed from `os.environ`:
116+
117+
```ini
118+
[pytest]
119+
env =
120+
U:DATABASE_URL
121+
```

0 commit comments

Comments
 (0)