Skip to content

Commit a65edf6

Browse files
authored
Merge pull request #5028 from blueyed/fix-wrap_session-exit-code
wrap_session: restore old behavior for initstate=1
2 parents e88aa95 + cc90bcc commit a65edf6

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/_pytest/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,13 @@ def wrap_session(config, doit):
214214
except (KeyboardInterrupt, exit.Exception):
215215
excinfo = _pytest._code.ExceptionInfo.from_current()
216216
exitstatus = EXIT_INTERRUPTED
217-
if initstate <= 2 and isinstance(excinfo.value, exit.Exception):
218-
sys.stderr.write("{}: {}\n".format(excinfo.typename, excinfo.value.msg))
217+
if isinstance(excinfo.value, exit.Exception):
219218
if excinfo.value.returncode is not None:
220219
exitstatus = excinfo.value.returncode
220+
if initstate < 2:
221+
sys.stderr.write(
222+
"{}: {}\n".format(excinfo.typename, excinfo.value.msg)
223+
)
221224
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
222225
session.exitstatus = exitstatus
223226
except: # noqa

testing/test_pdb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,8 @@ def test_3():
10161016
rest = child.read().decode("utf8")
10171017
assert "2 passed in" in rest
10181018
assert "reading from stdin while output" not in rest
1019-
assert "Exit: Quitting debugger" in child.before.decode("utf8")
1019+
# Only printed once - not on stderr.
1020+
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
10201021
TestPDB.flush(child)
10211022

10221023

testing/test_runner.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,24 @@ def test_foo():
580580
"""
581581
)
582582
result = testdir.runpytest()
583+
result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"])
584+
assert result.stderr.lines == [""]
583585
assert result.ret == 99
584586

587+
# It prints to stderr also in case of exit during pytest_sessionstart.
588+
testdir.makeconftest(
589+
"""
590+
import pytest
591+
592+
def pytest_sessionstart():
593+
pytest.exit("during_sessionstart", 98)
594+
"""
595+
)
596+
result = testdir.runpytest()
597+
result.stdout.fnmatch_lines(["*! *Exit: during_sessionstart !*"])
598+
assert result.stderr.lines == ["Exit: during_sessionstart", ""]
599+
assert result.ret == 98
600+
585601

586602
def test_pytest_fail_notrace_runtest(testdir):
587603
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""

0 commit comments

Comments
 (0)