Handle `Exit` exception in `pytest_sessionfinish`
Similar to a7268aa
(https://github.com/pytest-dev/pytest/pull/6258).
This commit is contained in:
parent
8bd612b367
commit
99d162e44a
|
@ -0,0 +1 @@
|
|||
:func:`pytest.exit() <_pytest.outcomes.exit>` is handled when emitted from the :func:`pytest_sessionfinish <_pytest.hookspec.pytest_sessionfinish>` hook. This includes quitting from a debugger.
|
|
@ -244,9 +244,14 @@ def wrap_session(
|
|||
excinfo = None # type: ignore
|
||||
session.startdir.chdir()
|
||||
if initstate >= 2:
|
||||
config.hook.pytest_sessionfinish(
|
||||
session=session, exitstatus=session.exitstatus
|
||||
)
|
||||
try:
|
||||
config.hook.pytest_sessionfinish(
|
||||
session=session, exitstatus=session.exitstatus
|
||||
)
|
||||
except Exit as exc:
|
||||
if exc.returncode is not None:
|
||||
session.exitstatus = exc.returncode
|
||||
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
|
||||
config._ensure_unconfigure()
|
||||
return session.exitstatus
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
from _pytest.main import ExitCode
|
||||
from _pytest.pytester import Testdir
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -50,3 +53,25 @@ def test_wrap_session_notify_exception(ret_exc, testdir):
|
|||
assert result.stderr.lines == ["mainloop: caught unexpected SystemExit!"]
|
||||
else:
|
||||
assert result.stderr.lines == ["Exit: exiting after {}...".format(exc.__name__)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("returncode", (None, 42))
|
||||
def test_wrap_session_exit_sessionfinish(
|
||||
returncode: Optional[int], testdir: Testdir
|
||||
) -> None:
|
||||
testdir.makeconftest(
|
||||
"""
|
||||
import pytest
|
||||
def pytest_sessionfinish():
|
||||
pytest.exit(msg="exit_pytest_sessionfinish", returncode={returncode})
|
||||
""".format(
|
||||
returncode=returncode
|
||||
)
|
||||
)
|
||||
result = testdir.runpytest()
|
||||
if returncode:
|
||||
assert result.ret == returncode
|
||||
else:
|
||||
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
||||
assert result.stdout.lines[-1] == "collected 0 items"
|
||||
assert result.stderr.lines == ["Exit: exit_pytest_sessionfinish"]
|
||||
|
|
Loading…
Reference in New Issue