Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added printing of captured stdout before entering pdb #3186

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Benjamin Peterson
Bernard Pratz
Bob Ippolito
Brian Dorsey
Brian Maissy
Brian Okken
Brianna Laugher
Bruno Oliveira
Expand Down
11 changes: 11 additions & 0 deletions _pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ def _enter_pdb(node, excinfo, rep):
# for not completely clear reasons.
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
tw.line()

captured_stdout = rep.capstdout
if len(captured_stdout) > 0:
tw.sep(">", "captured stdout")
tw.line(captured_stdout)

captured_stderr = rep.capstderr
if len(captured_stderr) > 0:
tw.sep(">", "captured stderr")
tw.line(captured_stderr)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also output the captured logs (rep.caplog) and make sure that they are not output if live-logging is enabled (-o log_cli=True -o log_level=DEBUG)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that rep.caplog was added in the features branch. So you can't use it if you merge your branch into master, but you can copy'n'paste the implemenation of BaseReport.caplog from https://github.com/pytest-dev/pytest/pull/3156/files#diff-e1a5c52f502faf325466018a5440c400R260.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also output the captured logs (rep.caplog) and make sure that they are not output if live-logging is enabled (-o log_cli=True -o log_level=DEBUG)

Why should we make a special case for living log here? I kind see the point, but we are dumping captured stdout/stderr at this point it follows that we should dump caplog then as well, regardless of live logging.

but you can copy'n'paste the implemenation of BaseReport.caplog from

If we end up doing this, please don't make caplog public: we don't want to introduce a new public API in a bug-fix release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be better to merge this and open a new issue to implement the same thing for caplog, in the features branch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brianmaissy fair enough. @Thisch do you agree?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let's do it like this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a new issue #3204

tw.sep(">", "traceback")
rep.toterminal(tw)
tw.sep(">", "entering PDB")
Expand Down
1 change: 1 addition & 0 deletions changelog/3052.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added printing of captured stdout/stderr before entering pdb, and improved a test which was giving false negatives about output capturing.
39 changes: 35 additions & 4 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,50 @@ def test_one(self):
child.sendeof()
self.flush(child)

def test_pdb_interaction_capture(self, testdir):
def test_pdb_print_captured_stdout(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
print("getrekt")
print("get\\x20rekt")
assert False
""")
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("getrekt")
child.expect("captured stdout")
child.expect("get rekt")
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
assert "getrekt" not in rest
assert "get rekt" not in rest
self.flush(child)

def test_pdb_print_captured_stderr(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
import sys
sys.stderr.write("get\\x20rekt")
assert False
""")
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("captured stderr")
child.expect("get rekt")
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
assert "get rekt" not in rest
self.flush(child)

def test_pdb_dont_print_empty_captured_stdout_and_stderr(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
assert False
""")
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("(Pdb)")
output = child.before.decode("utf8")
child.sendeof()
assert "captured stdout" not in output
assert "captured stderr" not in output
self.flush(child)

def test_pdb_interaction_exception(self, testdir):
Expand Down