Skip to content

Commit

Permalink
Merge pull request #6009 from yoavcaspi/fix_keyboardInterrupt_on_setu…
Browse files Browse the repository at this point in the history
…p_show

setuponly: remove printing out/err from capman
  • Loading branch information
nicoddemus committed Oct 20, 2019
2 parents b88f5df + 5624e36 commit 16efa1b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,6 @@ Wouter van Ackooy
Xixi Zhao
Xuan Luong
Xuecong Liao
Yoav Caspi
Zac Hatfield-Dodds
Zoltán Máté
1 change: 1 addition & 0 deletions changelog/5906.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash with ``KeyboardInterrupt`` during ``--setup-show``.
5 changes: 0 additions & 5 deletions src/_pytest/setuponly.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

import pytest


Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions testing/python/setup_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,47 @@ def test_arg(arg):
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*",
"*! KeyboardInterrupt !*",
"*= no tests ran in *",
]
)


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 !*", "*= no tests ran in *"]
)

0 comments on commit 16efa1b

Please sign in to comment.