File tree 3 files changed +23
-3
lines changed 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):
214
214
except (KeyboardInterrupt , exit .Exception ):
215
215
excinfo = _pytest ._code .ExceptionInfo .from_current ()
216
216
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 ):
219
218
if excinfo .value .returncode is not None :
220
219
exitstatus = excinfo .value .returncode
220
+ if initstate < 2 :
221
+ sys .stderr .write (
222
+ "{}: {}\n " .format (excinfo .typename , excinfo .value .msg )
223
+ )
221
224
config .hook .pytest_keyboard_interrupt (excinfo = excinfo )
222
225
session .exitstatus = exitstatus
223
226
except : # noqa
Original file line number Diff line number Diff line change @@ -1016,7 +1016,8 @@ def test_3():
1016
1016
rest = child .read ().decode ("utf8" )
1017
1017
assert "2 passed in" in rest
1018
1018
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" )
1020
1021
TestPDB .flush (child )
1021
1022
1022
1023
Original file line number Diff line number Diff line change @@ -580,8 +580,24 @@ def test_foo():
580
580
"""
581
581
)
582
582
result = testdir .runpytest ()
583
+ result .stdout .fnmatch_lines (["*! *Exit: some exit msg !*" ])
584
+ assert result .stderr .lines == ["" ]
583
585
assert result .ret == 99
584
586
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
+
585
601
586
602
def test_pytest_fail_notrace_runtest (testdir ):
587
603
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
You can’t perform that action at this time.
0 commit comments