Skip to content

Commit 13ad2a7

Browse files
committed
pytester: error on non-str pytester.plugins in subprocess mode instead of silently ignoring
In subprocess mode, adding a non-str plugin object to `pytester.plugins` can't work. Previously, such plugins would just be silently ignored. Silently ignoring an explicit setup doesn't seem right. Error instead.
1 parent 3e35a09 commit 13ad2a7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

changelog/13522.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
Fixed :fixture:`pytester` in subprocess mode ignored all :attr`pytester.plugins <pytest.Pytester.plugins>` except the first.
2+
3+
Fixed :fixture:`pytester` in subprocess mode silently ignored non-str :attr:`pytester.plugins <pytest.Pytester.plugins>`.
4+
Now it errors instead.
5+
If you are affected by this, specify the plugin by name, or switch the affected tests to use :func:`pytester.runpytest_inprocess <pytest.Pytester.runpytest_inprocess>` explicitly instead.

src/_pytest/pytester.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,11 @@ def __init__(
682682
self._name = name
683683
self._path: Path = tmp_path_factory.mktemp(name, numbered=True)
684684
#: A list of plugins to use with :py:meth:`parseconfig` and
685-
#: :py:meth:`runpytest`. Initially this is an empty list but plugins can
686-
#: be added to the list. The type of items to add to the list depends on
687-
#: the method using them so refer to them for details.
685+
#: :py:meth:`runpytest`. Initially this is an empty list but plugins can
686+
#: be added to the list.
687+
#:
688+
#: When running in subprocess mode, specify plugins by name (str) - adding
689+
#: plugin objects directly is not supported.
688690
self.plugins: list[str | _PluggyPlugin] = []
689691
self._sys_path_snapshot = SysPathsSnapshot()
690692
self._sys_modules_snapshot = self.__take_sys_modules_snapshot()
@@ -1494,8 +1496,12 @@ def runpytest_subprocess(
14941496
p = make_numbered_dir(root=self.path, prefix="runpytest-", mode=0o700)
14951497
args = (f"--basetemp={p}", *args)
14961498
for plugin in self.plugins:
1497-
if isinstance(plugin, str):
1498-
args = ("-p", plugin, *args)
1499+
if not isinstance(plugin, str):
1500+
raise ValueError(
1501+
f"Specifying plugins as objects is not supported in pytester subprocess mode; "
1502+
f"specify by name instead: {plugin}"
1503+
)
1504+
args = ("-p", plugin, *args)
14991505
args = self._getpytestargs() + args
15001506
return self.run(*args, timeout=timeout)
15011507

0 commit comments

Comments
 (0)