Skip to content

Commit 6653d39

Browse files
committed
📝 docs(readme): document TOML config file discovery
1 parent d9a315a commit 6653d39

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

README.md

Lines changed: 37 additions & 13 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,19 @@ 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 }
3636
```
3737

3838
In `pytest.toml` (or `.pytest.toml`):
@@ -41,17 +41,41 @@ In `pytest.toml` (or `.pytest.toml`):
4141
[pytest_env]
4242
HOME = "~/tmp"
4343
RUN_ENV = 1
44-
TRANSFORMED = {value = "{USER}/alpha", transform = true}
45-
SKIP_IF_SET = {value = "on", skip_if_set = true}
44+
TRANSFORMED = { value = "{USER}/alpha", transform = true }
45+
SKIP_IF_SET = { value = "on", skip_if_set = true }
4646
```
4747

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

5051
- if an inline table you can set options via the `transform` or `skip_if_set` keys, while the `value` key holds the
5152
value to set (or transform before setting). For transformation the variables you can use is other environment
5253
variable,
5354
- otherwise the value to set for the environment variable to set (casted to a string).
5455

56+
### Configuration file discovery
57+
58+
When using the native TOML form, `pytest-env` walks the directory tree starting from the directory containing the
59+
configuration file pytest resolved (`inipath`). For each directory it checks, in order: `pytest.toml`, `.pytest.toml`,
60+
and `pyproject.toml`. It stops at the first file that contains a `[pytest_env]` (or `[tool.pytest_env]` for
61+
`pyproject.toml`) section.
62+
63+
This means a subdirectory with its own `pytest.toml` containing `[pytest_env]` takes precedence over a parent
64+
`pyproject.toml` with `[tool.pytest_env]`, as long as pytest resolves its config to that subdirectory:
65+
66+
```
67+
project/
68+
├── pyproject.toml # [tool.pytest_env] DB_HOST = "prod-db"
69+
└── tests_integration/
70+
├── pytest.toml # [pytest_env] DB_HOST = "test-db"
71+
└── test_api.py
72+
```
73+
74+
Running `pytest tests_integration/` uses `DB_HOST = "test-db"` from the subdirectory config.
75+
76+
If no TOML file with a `pytest_env` section is found, the plugin falls back to the INI-style `env` key from pytest
77+
configuration.
78+
5579
### Via pytest configurations
5680

5781
In your pytest.ini file add a key value pair with `env` as the key and the environment variables as a line separated
@@ -69,8 +93,8 @@ Or with `pyproject.toml`:
6993
```toml
7094
[tool.pytest.ini_options]
7195
env = [
72-
"HOME=~/tmp",
73-
"RUN_ENV=test",
96+
"HOME=~/tmp",
97+
"RUN_ENV=test",
7498
]
7599
```
76100

0 commit comments

Comments
 (0)