Skip to content

Commit e05b33e

Browse files
committed
setuponly: remove printing out/err from capman
1 parent 2e11ea6 commit e05b33e

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,6 @@ Wouter van Ackooy
267267
Xixi Zhao
268268
Xuan Luong
269269
Xuecong Liao
270+
Yoav Caspi
270271
Zac Hatfield-Dodds
271272
Zoltán Máté

changelog/5906.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash with ``KeyboardInterrupt`` during ``--setup-show``.

src/_pytest/setuponly.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import sys
2-
31
import pytest
42

53

@@ -51,7 +49,6 @@ def _show_fixture_action(fixturedef, msg):
5149
capman = config.pluginmanager.getplugin("capturemanager")
5250
if capman:
5351
capman.suspend_global_capture()
54-
out, err = capman.read_global_capture()
5552

5653
tw = config.get_terminal_writer()
5754
tw.line()
@@ -74,8 +71,6 @@ def _show_fixture_action(fixturedef, msg):
7471

7572
if capman:
7673
capman.resume_global_capture()
77-
sys.stdout.write(out)
78-
sys.stderr.write(err)
7974

8075

8176
@pytest.hookimpl(tryfirst=True)

testing/python/setup_only.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,39 @@ def test_arg(arg):
267267
result.stdout.fnmatch_lines(
268268
["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"]
269269
)
270+
271+
272+
def test_setup_show_with_KeyboardInterrupt_in_test(testdir):
273+
""" Verifies that setups are shown and tests are executed even if there was a KeyboardInterrupt in a test. """
274+
p = testdir.makepyfile(
275+
"""
276+
import pytest
277+
@pytest.fixture
278+
def arg():
279+
assert True
280+
def test_arg(arg):
281+
raise KeyboardInterrupt()
282+
"""
283+
)
284+
result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True)
285+
assert result.ret == 2
286+
result.stdout.fnmatch_lines(
287+
["*SETUP F arg*", "*test_arg (fixtures used: arg)*", "*TEARDOWN F arg*"]
288+
)
289+
290+
291+
def test_setup_show_with_KeyboardInterrupt_in_fixture(testdir):
292+
""" Verifies that setups are shown and tests are executed even if there was a KeyboardInterrupt in a fixture. """
293+
p = testdir.makepyfile(
294+
"""
295+
import pytest
296+
@pytest.fixture
297+
def arg():
298+
raise KeyboardInterrupt()
299+
def test_arg(arg):
300+
assert True
301+
"""
302+
)
303+
result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True)
304+
assert result.ret == 2
305+
result.stdout.fnmatch_lines(["*SETUP F arg*", "*KeyboardInterrupt*"])

0 commit comments

Comments
 (0)