Skip to content

[pytest_env] in a custom-named TOML file passed with -c is ignored #326

Closed

Description

@gaborbernat

pytest -c ci.toml reads pytest's own settings from ci.toml, and pytest-env ignores the [pytest_env] table in the same file. It applies the table of a sibling or parent pyproject.toml, pytest.toml or .pytest.toml instead, or nothing when there is none. The run passes.

Affected code

Mechanism

pytest loads a TOML file with any name when it is passed with -c, and reads [pytest] from it like it does for pytest.toml. _find_toml_config accepts inipath only when it is named pytest.toml, .pytest.toml or pyproject.toml. For any other name it walks the directory and its parents for those three names, so the selected file is not opened. env_files and env_files_skip_if_set in the ignored table have no effect either, because they come from the same lookup.

Reproduction

repro.sh

cd "$(mktemp -d)"
printf '[pytest_env]\nDB_HOST = "ci-db"\n' > ci.toml
printf '[tool.pytest_env]\nDB_HOST = "dev-db"\n' > pyproject.toml
printf 'import os\n\n\ndef test_host() -> None:\n    print(os.environ.get("DB_HOST"))\n' > test_host.py
pytest -q -s -c ci.toml
mv ci.toml pytest.toml
pytest -q -s -c pytest.toml

Run with sh repro.sh at 3ebb6b4872fd0e450b6670d529e375e11c218e61, with pytest 9.2.0, python-dotenv 1.2.4 and CPython 3.14 on Linux:

dev-db
.
1 passed in 0.00s
ci-db
.
1 passed in 0.00s

Expected: DB_HOST is ci-db in both runs.

Impact

A project that keeps a separate configuration for CI runs its tests with the variables of the developer configuration.

  • Reproducibility: moderate. It needs -c with a TOML file that is not named pytest.toml, .pytest.toml or pyproject.toml.
  • Real-world exposure: niche. pytest accepts such a file since 9.2, and the README lists only the three fixed names for the native table.

Credits

Reported by Anthropic's OSS Scanner: ANT-2026-0WEHVFMQ.

Activity

  1. added
    bug
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions