From e05b33ed162fd9da8f9c44c9de035a0fa14631ec Mon Sep 17 00:00:00 2001 From: Yoav Caspi Date: Sat, 19 Oct 2019 22:28:06 +0300 Subject: [PATCH 1/2] setuponly: remove printing out/err from capman --- AUTHORS | 1 + changelog/5906.bugfix.rst | 1 + src/_pytest/setuponly.py | 5 ----- testing/python/setup_only.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 changelog/5906.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 9f6ee048d..e11400c1f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -267,5 +267,6 @@ Wouter van Ackooy Xixi Zhao Xuan Luong Xuecong Liao +Yoav Caspi Zac Hatfield-Dodds Zoltán Máté diff --git a/changelog/5906.bugfix.rst b/changelog/5906.bugfix.rst new file mode 100644 index 000000000..f5ded6cd2 --- /dev/null +++ b/changelog/5906.bugfix.rst @@ -0,0 +1 @@ +Fix crash with ``KeyboardInterrupt`` during ``--setup-show``. diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index 70d6ed12f..639349748 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -1,5 +1,3 @@ -import sys - import pytest @@ -51,7 +49,6 @@ def _show_fixture_action(fixturedef, msg): capman = config.pluginmanager.getplugin("capturemanager") if capman: capman.suspend_global_capture() - out, err = capman.read_global_capture() tw = config.get_terminal_writer() tw.line() @@ -74,8 +71,6 @@ def _show_fixture_action(fixturedef, msg): if capman: capman.resume_global_capture() - sys.stdout.write(out) - sys.stderr.write(err) @pytest.hookimpl(tryfirst=True) diff --git a/testing/python/setup_only.py b/testing/python/setup_only.py index 7c871a9ee..1c3a1d368 100644 --- a/testing/python/setup_only.py +++ b/testing/python/setup_only.py @@ -267,3 +267,39 @@ def test_show_fixtures_and_execute_test(testdir): result.stdout.fnmatch_lines( ["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"] ) + + +def test_setup_show_with_KeyboardInterrupt_in_test(testdir): + """ Verifies that setups are shown and tests are executed even if there was a KeyboardInterrupt in a test. """ + p = testdir.makepyfile( + """ + import pytest + @pytest.fixture + def arg(): + assert True + def test_arg(arg): + raise KeyboardInterrupt() + """ + ) + result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True) + assert result.ret == 2 + result.stdout.fnmatch_lines( + ["*SETUP F arg*", "*test_arg (fixtures used: arg)*", "*TEARDOWN F arg*"] + ) + + +def test_setup_show_with_KeyboardInterrupt_in_fixture(testdir): + """ Verifies that setups are shown and tests are executed even if there was a KeyboardInterrupt in a fixture. """ + p = testdir.makepyfile( + """ + import pytest + @pytest.fixture + def arg(): + raise KeyboardInterrupt() + def test_arg(arg): + assert True + """ + ) + result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True) + assert result.ret == 2 + result.stdout.fnmatch_lines(["*SETUP F arg*", "*KeyboardInterrupt*"]) From 5624e366c1c812e200231e70c6ba2f15dc04a323 Mon Sep 17 00:00:00 2001 From: Yoav Caspi Date: Sun, 20 Oct 2019 09:54:23 +0300 Subject: [PATCH 2/2] add more indications to the result of the tests --- testing/python/setup_only.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/testing/python/setup_only.py b/testing/python/setup_only.py index 1c3a1d368..8e4e3ace8 100644 --- a/testing/python/setup_only.py +++ b/testing/python/setup_only.py @@ -284,7 +284,13 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir): result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True) assert result.ret == 2 result.stdout.fnmatch_lines( - ["*SETUP F arg*", "*test_arg (fixtures used: arg)*", "*TEARDOWN F arg*"] + [ + "*SETUP F arg*", + "*test_arg (fixtures used: arg)*", + "*TEARDOWN F arg*", + "*! KeyboardInterrupt !*", + "*= no tests ran in *", + ] ) @@ -302,4 +308,6 @@ def test_setup_show_with_KeyboardInterrupt_in_fixture(testdir): ) result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True) assert result.ret == 2 - result.stdout.fnmatch_lines(["*SETUP F arg*", "*KeyboardInterrupt*"]) + result.stdout.fnmatch_lines( + ["*SETUP F arg*", "*! KeyboardInterrupt !*", "*= no tests ran in *"] + )