Skip to content

Commit 6bc45d1

Browse files
authored
Merge pull request #3186 from brianmaissy/bugfix/print_captured_stdout_before_entering_pdb
Added printing of captured stdout before entering pdb
2 parents 371eb8c + 7656fc8 commit 6bc45d1

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
@@ -86,6 +86,17 @@ def _enter_pdb(node, excinfo, rep):
8686
# for not completely clear reasons.
8787
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
8888
tw.line()
89+
90+
captured_stdout = rep.capstdout
91+
if len(captured_stdout) > 0:
92+
tw.sep(">", "captured stdout")
93+
tw.line(captured_stdout)
94+
95+
captured_stderr = rep.capstderr
96+
if len(captured_stderr) > 0:
97+
tw.sep(">", "captured stderr")
98+
tw.line(captured_stderr)
99+
89100
tw.sep(">", "traceback")
90101
rep.toterminal(tw)
91102
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)