Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/8986.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Emit a :class:`~pytest.PytestConfigWarning` when a plugin raises ``SystemExit`` in :hook:`pytest_cmdline_main`.
12 changes: 11 additions & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,17 @@ def _main(
return ExitCode.INTERNAL_ERROR

try:
ret: ExitCode | int = config.hook.pytest_cmdline_main(config=config)
try:
ret: ExitCode | int = config.hook.pytest_cmdline_main(config=config)
except SystemExit:
warnings.warn(
PytestConfigWarning(
"A plugin raised SystemExit in pytest_cmdline_main; "
"pytest_cmdline_main implementations should return an ExitCode or int instead."
),
stacklevel=2,
)
raise
try:
return ExitCode(ret)
except ValueError:
Expand Down
18 changes: 18 additions & 0 deletions testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,21 @@ def test_foo(specified_feeds):
)
result = pytester.runpytest("--long-list", ",".join(["helloworld"] * 100_000))
result.stdout.fnmatch_lines("* 1 passed *")


def test_cmdline_main_systemexit_warning() -> None:
"""Warn when a plugin raises SystemExit in pytest_cmdline_main (#8986)."""

class Plugin:
def pytest_cmdline_main(self) -> int:
raise SystemExit(2)

with (
pytest.warns(
pytest.PytestConfigWarning,
match="A plugin raised SystemExit in pytest_cmdline_main",
),
pytest.raises(SystemExit) as excinfo,
):
pytest.main([], plugins=[Plugin()])
assert excinfo.value.code == 2
Loading