From cc90bcce4c3d6bc55aeb85a6450020cb4a2e49f2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 3 Apr 2019 04:07:42 +0200 Subject: [PATCH] wrap_session: restore old behavior for initstate=1 --- src/_pytest/main.py | 7 +++++-- testing/test_pdb.py | 3 ++- testing/test_runner.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 19553ca0e..df4a7a956 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -214,10 +214,13 @@ def wrap_session(config, doit): except (KeyboardInterrupt, exit.Exception): excinfo = _pytest._code.ExceptionInfo.from_current() exitstatus = EXIT_INTERRUPTED - if initstate <= 2 and isinstance(excinfo.value, exit.Exception): - sys.stderr.write("{}: {}\n".format(excinfo.typename, excinfo.value.msg)) + if isinstance(excinfo.value, exit.Exception): if excinfo.value.returncode is not None: exitstatus = excinfo.value.returncode + if initstate < 2: + sys.stderr.write( + "{}: {}\n".format(excinfo.typename, excinfo.value.msg) + ) config.hook.pytest_keyboard_interrupt(excinfo=excinfo) session.exitstatus = exitstatus except: # noqa diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 531846e8e..f524f06a2 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -1015,7 +1015,8 @@ class TestTraceOption: rest = child.read().decode("utf8") assert "2 passed in" in rest assert "reading from stdin while output" not in rest - assert "Exit: Quitting debugger" in child.before.decode("utf8") + # Only printed once - not on stderr. + assert "Exit: Quitting debugger" not in child.before.decode("utf8") TestPDB.flush(child) diff --git a/testing/test_runner.py b/testing/test_runner.py index 72484fb72..cf335dfad 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -580,8 +580,24 @@ def test_pytest_exit_returncode(testdir): """ ) result = testdir.runpytest() + result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"]) + assert result.stderr.lines == [""] assert result.ret == 99 + # It prints to stderr also in case of exit during pytest_sessionstart. + testdir.makeconftest( + """ + import pytest + + def pytest_sessionstart(): + pytest.exit("during_sessionstart", 98) + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*! *Exit: during_sessionstart !*"]) + assert result.stderr.lines == ["Exit: during_sessionstart", ""] + assert result.ret == 98 + def test_pytest_fail_notrace_runtest(testdir): """Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""