Skip to content

Commit 30edc39

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 76d62a3 commit 30edc39

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-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

testing/test_pytester.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,26 @@ def test_two():
834834
result.assert_outcomes(passed=1, deselected=1)
835835
# If deselected is not passed, it is not checked at all.
836836
result.assert_outcomes(passed=1)
837+
838+
839+
def test_pytester_subprocess_with_string_plugins(pytester: Pytester) -> None:
840+
"""Test that pytester.runpytest_subprocess is OK with named (string)
841+
`.plugins`."""
842+
843+
pytester.plugins = ["pytester"]
844+
845+
result = pytester.runpytest_subprocess()
846+
assert result.ret == ExitCode.NO_TESTS_COLLECTED
847+
848+
849+
def test_pytester_subprocess_with_non_string_plugins(pytester: Pytester) -> None:
850+
"""Test that pytester.runpytest_subprocess fails with a proper error given
851+
non-string `.plugins`."""
852+
853+
class MyPlugin:
854+
pass
855+
856+
pytester.plugins = [MyPlugin()]
857+
858+
with pytest.raises(ValueError, match="plugins as objects is not supported"):
859+
pytester.runpytest_subprocess()

0 commit comments

Comments
 (0)