Skip to content

print captured logs before entering pdb #3210

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

Closed
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
6 changes: 6 additions & 0 deletions _pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def _enter_pdb(node, excinfo, rep):
tw.sep(">", "captured stderr")
tw.line(captured_stderr)

captured_logs = rep.caplog
if len(captured_logs) > 0:
if node.config.getoption('log_print') is not False:
tw.sep(">", "captured logs")
tw.line(captured_logs)

tw.sep(">", "traceback")
rep.toterminal(tw)
tw.sep(">", "entering PDB")
Expand Down
1 change: 1 addition & 0 deletions changelog/3204.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Captured logs are printed before entering pdb, unless ``--no-print-logs`` is defined.
50 changes: 50 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,56 @@ def test_1():
assert "captured stderr" not in output
self.flush(child)

def test_pdb_print_captured_logs(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
import logging
logging.warn("get rekt")
assert False
""")
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("captured logs")
child.expect("get rekt")
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
self.flush(child)

def test_pdb_dont_print_captured_logs_when_no_print_logs_is_defined(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
import logging
logging.warn("get rekt")
assert False
""")
child = testdir.spawn_pytest("--pdb --no-print-logs %s" % p1)
child.expect("get rekt")
output = child.before.decode("utf8")
assert "captured logs" not in output
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
self.flush(child)

def test_pdb_doesnt_print_captured_logs_when_logging_plugin_is_disabled(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
import logging
logging.warn("get rekt")
assert False
""")
child = testdir.spawn_pytest("--pdb -p no:logging %s" % p1)
child.expect("get rekt")
output = child.before.decode("utf8")
assert "captured logs" not in output
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
self.flush(child)

def test_pdb_interaction_exception(self, testdir):
p1 = testdir.makepyfile("""
import pytest
Expand Down