Skip to content

Commit

Permalink
account for logging-plugging being disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoban13 committed May 13, 2023
1 parent b53743c commit a856590
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/pytest_subtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ def _capturing_output(self):
@contextmanager
def _capturing_logs(self):
logging_plugin = self.request.config.pluginmanager.getplugin("logging-plugin")
handler = LogCaptureHandler()
handler.setFormatter(logging_plugin.formatter)

captured_logs = CapturedLogs(handler)
with catching_logs(handler):
yield captured_logs
if logging_plugin is None:
yield NullCapturedLogs()
else:
handler = LogCaptureHandler()
handler.setFormatter(logging_plugin.formatter)

captured_logs = CapturedLogs(handler)
with catching_logs(handler):
yield captured_logs

@contextmanager
def test(self, msg=None, **kwargs):
Expand All @@ -211,8 +214,8 @@ def test(self, msg=None, **kwargs):
sub_report = SubTestReport._from_test_report(report)
sub_report.context = SubTestContext(msg, kwargs.copy())

captured.update_report(sub_report)
capturedlogs.update_report(sub_report)
captured_output.update_report(sub_report)
captured_logs.update_report(sub_report)

with self.suspend_capture_ctx():
self.ihook.pytest_runtest_logreport(report=sub_report)
Expand Down Expand Up @@ -259,12 +262,18 @@ def update_report(self, report):
if self.err:
report.sections.append(("Captured stderr call", self.err))


class CapturedLogs:
def __init__(self, handler):
self._handler = handler

def update_report(self, report):
report.sections.append(("Captured log call", self._handler.stream.getvalue()))


class NullCapturedLogs:
def update_report(self, report):
pass


def pytest_report_to_serializable(report):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_subtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,32 @@ def test(subtests, caplog):
]
)

def test_no_logging(self, testdir):
testdir.makepyfile(
"""
import logging
def test(subtests):
logging.info("start log line")
with subtests.test("sub passing"):
logging.info("inside %s", "passing log line")
with subtests.test("sub failing"):
logging.info("inside %s", "failing log line")
assert False
logging.info("end log line")
"""
)
result = testdir.runpytest("-p no:logging")
result.stdout.fnmatch_lines(
[
"*1 passed*",
]
)
result.stdout.no_fnmatch_line("*root:test_no_logging.py*log line*")


class TestDebugging:
"""Check --pdb support for subtests fixture and TestCase.subTest."""
Expand Down

0 comments on commit a856590

Please sign in to comment.