✅ test(plugin): verify env vars available during conftest imports#194
Merged
gaborbernat merged 1 commit intopytest-dev:mainfrom Feb 11, 2026
Merged
✅ test(plugin): verify env vars available during conftest imports#194gaborbernat merged 1 commit intopytest-dev:mainfrom
gaborbernat merged 1 commit intopytest-dev:mainfrom
Conversation
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a concern that if
conftest.pycontains top-level imports of modules that read environment variables at import time, pytest-env's plugin mechanism might set those variables too late. This could causeKeyErrorcrashes during conftest loading before the test session even begins.Investigation of pytest's hook execution order confirmed that the current
@pytest.hookimpl(tryfirst=True)onpytest_load_initial_conftestsis correct — pytest's own conftest-loading implementation usestrylast=True, so pytest-env always sets variables before any conftest is imported. 🔍 This PR adds a regression test to codify that guarantee across all three config formats (ini, pyproject.toml, pytest.toml).The test creates a module that reads
os.environ['MAGIC']at import time, aconftest.pythat imports it at the top level, and verifies the value is available. The environment is cleared viamock.patch.dictwithclear=Trueto ensure the only source of the variable is pytest-env.