File tree Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -1015,7 +1015,8 @@ def test_3():
10151015 rest = child .read ().decode ("utf8" )
10161016 assert "2 passed in" in rest
10171017 assert "reading from stdin while output" not in rest
1018- assert "Exit: Quitting debugger" in child .before .decode ("utf8" )
1018+ # Only printed once - not on stderr.
1019+ assert "Exit: Quitting debugger" not in child .before .decode ("utf8" )
10191020 TestPDB .flush (child )
10201021
10211022
Original file line number Diff line number Diff 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
586602def test_pytest_fail_notrace_runtest (testdir ):
587603 """Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
You can’t perform that action at this time.
0 commit comments