Skip to content

Commit 6669073

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 b3ad282 commit 6669073

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()
@@ -1488,8 +1490,12 @@ def runpytest_subprocess(
14881490
p = make_numbered_dir(root=self.path, prefix="runpytest-", mode=0o700)
14891491
args = (f"--basetemp={p}", *args)
14901492
for plugin in self.plugins:
1491-
if isinstance(plugin, str):
1492-
args = ("-p", plugin, *args)
1493+
if not isinstance(plugin, str):
1494+
raise ValueError(
1495+
f"Specifying plugins as objects is not supported in pytester subprocess mode; "
1496+
f"specify by name instead: {plugin}"
1497+
)
1498+
args = ("-p", plugin, *args)
14931499
args = self._getpytestargs() + args
14941500
return self.run(*args, timeout=timeout)
14951501

0 commit comments

Comments
 (0)