Skip to content

Warn when pytest_plugins is defined in a test module - #15122

Open
lognd wants to merge 13 commits into
pytest-dev:mainfrom
lognd:warn-pytest-plugins-in-test-module
Open

lognd wants to merge 13 commits into
pytest-dev:mainfrom
lognd:warn-pytest-plugins-in-test-module

Conversation

@lognd

@lognd lognd commented Oct 2, 2026

Copy link
Copy Markdown

Summary

Defining pytest_plugins in 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 a PytestConfigWarning for such modules. The warning waits until a second test file is collected, so single-file plugin test suites (the pytest_plugins = ["pytester"] pattern listed in the thread) stay quiet (which was discussed in the issue). It is emitted from Module.collect rather than a session hook, so filterwarnings = error turns into a regular collection error instead of an INTERNALERROR.

The new warning also caught a redundant pytest_plugins = ("pytester",) in testing/test_cacheprovider.py, removed here since pytester is already enabled through addopts.

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.

lognd added 4 commits October 1, 2026 21:53
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
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 2, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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:

  1. The warning for the first module is raised while the second module is collected, so with -W error the collection error lands on the wrong file.
  2. Don't keep every Module in the stash. A count plus the first module is enough.
  3. Make it a PytestDeprecationWarning that isn't tied to a removal version, since this is meant to become a deprecation.

Generated by Claude Code

Comment thread src/_pytest/python.py Outdated
Comment thread src/_pytest/python.py Outdated
Comment thread src/_pytest/python.py Outdated
lognd and others added 7 commits October 13, 2026 01:56
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.
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should warn when using pytest_plugins in a test file (since it's also not the root conftest.py)

2 participants