Skip to content

Commit 5c0611c

Browse files
committed
Forward log_level from plugin
1 parent 8f1a038 commit 5c0611c

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/_pytest/subtests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from _pytest.fixtures import SubRequest
3232
from _pytest.logging import catching_logs
3333
from _pytest.logging import LogCaptureHandler
34+
from _pytest.logging import LoggingPlugin
3435
from _pytest.reports import TestReport
3536
from _pytest.runner import CallInfo
3637
from _pytest.runner import check_interactive_exception
@@ -317,15 +318,17 @@ def capturing_output(request: SubRequest) -> Iterator[Captured]:
317318
def capturing_logs(
318319
request: SubRequest,
319320
) -> Iterator[CapturedLogs | None]:
320-
logging_plugin = request.config.pluginmanager.getplugin("logging-plugin")
321+
logging_plugin: LoggingPlugin | None = request.config.pluginmanager.getplugin(
322+
"logging-plugin"
323+
)
321324
if logging_plugin is None:
322325
yield None
323326
else:
324327
handler = LogCaptureHandler()
325328
handler.setFormatter(logging_plugin.formatter)
326329

327330
captured_logs = CapturedLogs(handler)
328-
with catching_logs(handler):
331+
with catching_logs(handler, level=logging_plugin.log_level):
329332
yield captured_logs
330333

331334

testing/test_subtests.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,17 @@ def test_foo(subtests):
769769
with subtests.test("sub1"):
770770
print("sub1 stdout")
771771
logging.info("sub1 logging")
772+
logging.debug("sub1 logging debug")
772773
773774
with subtests.test("sub2"):
774775
print("sub2 stdout")
775776
logging.info("sub2 logging")
777+
logging.debug("sub2 logging debug")
776778
assert False
777779
"""
778780
)
779781

780-
def test_capturing(self, pytester: pytest.Pytester) -> None:
782+
def test_capturing_info(self, pytester: pytest.Pytester) -> None:
781783
self.create_file(pytester)
782784
result = pytester.runpytest("--log-level=INFO")
783785
result.stdout.fnmatch_lines(
@@ -786,7 +788,29 @@ def test_capturing(self, pytester: pytest.Pytester) -> None:
786788
"*-- Captured stdout call --*",
787789
"sub2 stdout",
788790
"*-- Captured log call ---*",
789-
"INFO root:test_capturing.py:12 sub2 logging",
791+
"INFO * before",
792+
"INFO * sub1 logging",
793+
"INFO * sub2 logging",
794+
"*== short test summary info ==*",
795+
]
796+
)
797+
result.stdout.no_fnmatch_line("sub1 logging debug")
798+
result.stdout.no_fnmatch_line("sub2 logging debug")
799+
800+
def test_capturing_debug(self, pytester: pytest.Pytester) -> None:
801+
self.create_file(pytester)
802+
result = pytester.runpytest("--log-level=DEBUG")
803+
result.stdout.fnmatch_lines(
804+
[
805+
"*___ test_foo [[]sub2[]] __*",
806+
"*-- Captured stdout call --*",
807+
"sub2 stdout",
808+
"*-- Captured log call ---*",
809+
"INFO * before",
810+
"INFO * sub1 logging",
811+
"DEBUG * sub1 logging debug",
812+
"INFO * sub2 logging",
813+
"DEBUG * sub2 logging debug",
790814
"*== short test summary info ==*",
791815
]
792816
)

0 commit comments

Comments
 (0)