Skip to content

✨ feat(config): merge nested pytest_env tables - #370

Merged
gaborbernat merged 3 commits into
mainfrom
merge-nested-env-tables
Dec 4, 2026
Merged

gaborbernat merged 3 commits into
mainfrom
merge-nested-env-tables

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

In a monorepo, pytest-env reads only the first pytest_env table above pytest's configuration directory, so a subproject's own table is ignored.

✨ With merge_nested = true in the root table, pytest-env also reads the tables in the directories below it, down to each path given on the command line (or the current directory; testpaths is not read):

  • A nested entry replaces the parent's entry for the same variable, including its transform, skip_if_set and unset keys.
  • A transform = true template applies after the variables it names, whichever table sets them, so a root DB_URL = { value = "pg://db/{DB_NAME}", transform = true } uses the subproject's DB_NAME. A cycle between templates is a usage error.
  • Nested env_files load after the parent's, relative to their own file; like all .env files, they don't override table entries.
  • A nested env_files_skip_if_set replaces the parent's.
  • Paths in one run that would read different tables, such as pytest packages/storage packages/logic, stop with a usage error, since they share one environment.

merge_nested becomes a reserved key; before, it was exported as a variable. Each entry now records its source file, which --pytest-env-verbose and the "cannot set environment variable" error report.

repo/
├── pyproject.toml          # [tool.pytest] minversion = "9"
│                           # [tool.pytest_env] merge_nested = true, APP_ENV = "test"
└── packages/
    ├── storage/
    │   └── pyproject.toml  # [tool.pytest_env] SOME_ENV_VAR = "A"
    └── logic/
        └── pyproject.toml  # [tool.pytest_env] SOME_ENV_VAR = "B"
$ pytest packages/storage --pytest-env-verbose
 pytest-env:
-  SET   merge_nested=True  (from repo/pyproject.toml)
   SET   APP_ENV=test  (from repo/pyproject.toml)
+  SET   SOME_ENV_VAR=A  (from repo/packages/storage/pyproject.toml)

Closes #369

In a monorepo one pytest configuration at the root serves several
subprojects, but pytest-env stops at the first pytest_env table it finds
walking up from the configuration directory, so a subproject's own table
is never read.

With merge_nested = true in that table, pytest-env also reads the tables
between it and each tested path and merges them from the top down. A
nested entry replaces the parent's entry for the same variable, flags
included, and each {VAR} template applies after the variables it names,
whichever table sets them, so a root URL template picks up a name a
subproject sets. Nested env_files load after the parent's, resolved from
their own directory, and a nested env_files_skip_if_set replaces the
parent's.

Paths that would read different tables in one run stop pytest with a
usage error, since they share one environment.

Closes #369
@gaborbernat gaborbernat added the enhancement New feature or request label Dec 4, 2026
@gaborbernat
gaborbernat merged commit d478a4f into main Dec 4, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the merge-nested-env-tables branch December 4, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merge [tool.pytest_env] tables from subprojects in a monorepo

1 participant