Repository navigation
Conversation
Defining ``pytest_plugins`` in a test module activates the plugins for the entire test suite, not just that module, the same problem that led to the non-top-level conftest check. Unlike the conftest case nothing was reported, which wrongly implied that test modules were safe. Emit a ``PytestConfigWarning`` from ``Module.collect`` once a second test module is seen, so single-file plugin test suites stay quiet and a ``filterwarnings = error`` setup gets a normal collection error. Refs pytest-dev#13030
Cover the warning, the single-file suite exemption, and the behavior under an error warning filter. Refs pytest-dev#13030
The pytester plugin is already enabled for the whole suite through addopts, and the module-level declaration now triggers the new warning, which pytest's own suite turns into an error.
Add the changelog entry, a note in the plugin-writing guide next to the existing non-root conftest note, and the AUTHORS entry. Closes pytest-dev#13030
RonnyPfannschmidt
approved these changes
Oct 2, 2026
RonnyPfannschmidt
requested changes
Oct 6, 2026
RonnyPfannschmidt
left a comment
Member
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Thanks, this matches the direction from #13030: warn whenever more than one test file is collected, and keep single-file plugin suites quiet. The extra warnings this brings for projects that repeat pytest_plugins = ("pytester",) in every test file are expected.
Three things to address, see the inline comments:
- The warning for the first module is raised while the second module is collected, so with
-W errorthe collection error lands on the wrong file. - Don't keep every
Modulein the stash. A count plus the first module is enough. - Make it a
PytestDeprecationWarningthat isn't tied to a removal version, since this is meant to become a deprecation.
Generated by Claude Code
Storing every collected Module in the config stash kept them all alive for the whole session. The check only needs to know how many modules were collected and which one came first, and the first one can be dropped once the second is collected.
The first collected module can only be warned about once a second one is collected. With -W error that surfaced as a collection error of the second module, even though the first one defines pytest_plugins. Report it as a collection error of the defining module instead, and test both the first and a later module.
The long-term direction is to deprecate pytest_plugins in test modules altogether, so emit a PytestDeprecationWarning instead of a config warning. It is not tied to a removal version yet. Document it in the deprecations page and turn the changelog entry into a deprecation.
…le' into warn-pytest-plugins-in-test-module
Collection reports are built from the node's structured id since pytest-dev#14758 (see runner.py and cacheprovider.py). Passing the string nodeid made CollectReport parse it back.
…le' into warn-pytest-plugins-in-test-module
This branch has not been deployed
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.
Summary
Defining
pytest_pluginsin a test module loads the plugins for the entire session, the same problem the non-top-level conftest check guards against, but nothing is reported, which implies test modules are safe. This emits aPytestConfigWarningfor such modules. The warning waits until a second test file is collected, so single-file plugin test suites (thepytest_plugins = ["pytester"]pattern listed in the thread) stay quiet (which was discussed in the issue). It is emitted fromModule.collectrather than a session hook, sofilterwarnings = errorturns into a regular collection error instead of an INTERNALERROR.The new warning also caught a redundant
pytest_plugins = ("pytester",)intesting/test_cacheprovider.py, removed here since pytester is already enabled throughaddopts.AI disclosure: I used Claude Code while working on this. It helped me map how plugin loading and collection work and checked the change against the repo's conventions. I reviewed everything, understand the change, and will respond to review myself.
Closes #13030
Test Plan
I have added tests for the warning, the single-file exemption, and the error-filter case, and run the full test suite. The changelog fragment, a docs note in the plugin-writing guide, and an AUTHORS entry are included.