Skip to content

Commit 7656fc8

Browse files
committed
Added printing of captured stdout and stderr before entering pdb
1 parent 8c81722 commit 7656fc8

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Benjamin Peterson
2929
Bernard Pratz
3030
Bob Ippolito
3131
Brian Dorsey
32+
Brian Maissy
3233
Brian Okken
3334
Brianna Laugher
3435
Bruno Oliveira

_pytest/debugging.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ def _enter_pdb(node, excinfo, rep):
8585
# for not completely clear reasons.
8686
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
8787
tw.line()
88+
89+
captured_stdout = rep.capstdout
90+
if len(captured_stdout) > 0:
91+
tw.sep(">", "captured stdout")
92+
tw.line(captured_stdout)
93+
94+
captured_stderr = rep.capstderr
95+
if len(captured_stderr) > 0:
96+
tw.sep(">", "captured stderr")
97+
tw.line(captured_stderr)
98+
8899
tw.sep(">", "traceback")
89100
rep.toterminal(tw)
90101
tw.sep(">", "entering PDB")

changelog/3052.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added printing of captured stdout/stderr before entering pdb, and improved a test which was giving false negatives about output capturing.

testing/test_pdb.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,50 @@ def test_one(self):
141141
child.sendeof()
142142
self.flush(child)
143143

144-
def test_pdb_interaction_capture(self, testdir):
144+
def test_pdb_print_captured_stdout(self, testdir):
145145
p1 = testdir.makepyfile("""
146146
def test_1():
147-
print("getrekt")
147+
print("get\\x20rekt")
148148
assert False
149149
""")
150150
child = testdir.spawn_pytest("--pdb %s" % p1)
151-
child.expect("getrekt")
151+
child.expect("captured stdout")
152+
child.expect("get rekt")
152153
child.expect("(Pdb)")
153154
child.sendeof()
154155
rest = child.read().decode("utf8")
155156
assert "1 failed" in rest
156-
assert "getrekt" not in rest
157+
assert "get rekt" not in rest
158+
self.flush(child)
159+
160+
def test_pdb_print_captured_stderr(self, testdir):
161+
p1 = testdir.makepyfile("""
162+
def test_1():
163+
import sys
164+
sys.stderr.write("get\\x20rekt")
165+
assert False
166+
""")
167+
child = testdir.spawn_pytest("--pdb %s" % p1)
168+
child.expect("captured stderr")
169+
child.expect("get rekt")
170+
child.expect("(Pdb)")
171+
child.sendeof()
172+
rest = child.read().decode("utf8")
173+
assert "1 failed" in rest
174+
assert "get rekt" not in rest
175+
self.flush(child)
176+
177+
def test_pdb_dont_print_empty_captured_stdout_and_stderr(self, testdir):
178+
p1 = testdir.makepyfile("""
179+
def test_1():
180+
assert False
181+
""")
182+
child = testdir.spawn_pytest("--pdb %s" % p1)
183+
child.expect("(Pdb)")
184+
output = child.before.decode("utf8")
185+
child.sendeof()
186+
assert "captured stdout" not in output
187+
assert "captured stderr" not in output
157188
self.flush(child)
158189

159190
def test_pdb_interaction_exception(self, testdir):

0 commit comments

Comments
 (0)