Skip to content

Commit c26f389

Browse files
committed
Refactor handling of non-top-level pytest_plugins handling
Decided to move the 'if' logic together with the error message, as this leaves the _importconftest method cleaner.
1 parent 9e1e7fc commit c26f389

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/_pytest/config/__init__.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,6 @@ def _prepareconfig(
286286
raise
287287

288288

289-
def _fail_on_non_top_pytest_plugins(conftestpath, confcutdir):
290-
msg = (
291-
"Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:\n"
292-
"It affects the entire test suite instead of just below the conftest as expected.\n"
293-
" {}\n"
294-
"Please move it to a top level conftest file at the rootdir:\n"
295-
" {}\n"
296-
"For more information, visit:\n"
297-
" https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files"
298-
)
299-
fail(msg.format(conftestpath, confcutdir), pytrace=False)
300-
301-
302289
class PytestPluginManager(PluginManager):
303290
"""
304291
Overwrites :py:class:`pluggy.PluginManager <pluggy.PluginManager>` to add pytest-specific
@@ -527,15 +514,11 @@ def _importconftest(self, conftestpath):
527514

528515
try:
529516
mod = conftestpath.pyimport()
530-
if (
531-
hasattr(mod, "pytest_plugins")
532-
and self._configured
533-
and not self._using_pyargs
534-
):
535-
_fail_on_non_top_pytest_plugins(conftestpath, self._confcutdir)
536517
except Exception as e:
537518
raise ConftestImportFailure(conftestpath, sys.exc_info()) from e
538519

520+
self._check_non_top_pytest_plugins(mod, conftestpath)
521+
539522
self._conftest_plugins.add(mod)
540523
self._conftestpath2mod[key] = mod
541524
dirpath = conftestpath.dirpath()
@@ -548,6 +531,23 @@ def _importconftest(self, conftestpath):
548531
self.consider_conftest(mod)
549532
return mod
550533

534+
def _check_non_top_pytest_plugins(self, mod, conftestpath):
535+
if (
536+
hasattr(mod, "pytest_plugins")
537+
and self._configured
538+
and not self._using_pyargs
539+
):
540+
msg = (
541+
"Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:\n"
542+
"It affects the entire test suite instead of just below the conftest as expected.\n"
543+
" {}\n"
544+
"Please move it to a top level conftest file at the rootdir:\n"
545+
" {}\n"
546+
"For more information, visit:\n"
547+
" https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files"
548+
)
549+
fail(msg.format(conftestpath, self._confcutdir), pytrace=False)
550+
551551
#
552552
# API for bootstrapping plugin loading
553553
#

0 commit comments

Comments
 (0)