diff --git a/src/pytest_subtests.py b/src/pytest_subtests.py index 98f18b6..2af2504 100644 --- a/src/pytest_subtests.py +++ b/src/pytest_subtests.py @@ -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): @@ -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) @@ -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): diff --git a/tests/test_subtests.py b/tests/test_subtests.py index 0311478..fc232ef 100644 --- a/tests/test_subtests.py +++ b/tests/test_subtests.py @@ -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."""