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

Log in subtests similar to stdout #91

Merged
merged 7 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 21 additions & 2 deletions pytest_subtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from _pytest.capture import CaptureFixture
from _pytest.capture import FDCapture
from _pytest.capture import SysCapture
from _pytest.logging import LogCaptureHandler, catching_logs
from _pytest.outcomes import OutcomeException
from _pytest.reports import TestReport
from _pytest.runner import CallInfo
Expand Down Expand Up @@ -155,14 +156,24 @@ def _capturing_output(self):
fixture.close()
captured.out = out
captured.err = err

@contextmanager
def _capturing_logs(self):
logging_plugin = self.request.config.pluginmanager.getplugin("logging-plugin")
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
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):
start = time.time()
precise_start = time.perf_counter()
exc_info = None

with self._capturing_output() as captured:
with self._capturing_output() as captured, self._capturing_logs() as capturedlogs:
rhoban13 marked this conversation as resolved.
Show resolved Hide resolved
try:
yield
except (Exception, OutcomeException):
Expand All @@ -180,7 +191,8 @@ def test(self, msg=None, **kwargs):
sub_report.context = SubTestContext(msg, kwargs.copy())

captured.update_report(sub_report)

capturedlogs.update_report(sub_report)

with self.suspend_capture_ctx():
self.ihook.pytest_runtest_logreport(report=sub_report)

Expand Down Expand Up @@ -226,6 +238,13 @@ 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()))


def pytest_report_to_serializable(report):
if isinstance(report, SubTestReport):
Expand Down
59 changes: 59 additions & 0 deletions tests/test_subtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,65 @@ def test(subtests, {fixture}):
)


class TestLogging:
def create_file(self, testdir):
testdir.makepyfile(
"""
import logging

def test_foo(subtests):
logging.info("before")

with subtests.test("sub1"):
print("sub1 stdout")
logging.info("sub1 logging")

with subtests.test("sub2"):
print("sub2 stdout")
logging.info("sub2 logging")
assert False
"""
)
rhoban13 marked this conversation as resolved.
Show resolved Hide resolved

def test_capturing(self, testdir):
self.create_file(testdir)
result = testdir.runpytest("--log-level=INFO")
result.stdout.fnmatch_lines(
[
"*___ test_foo [[]sub2[]] __*",
"*-- Captured stdout call --*",
"sub2 stdout",
"*-- Captured log call ---*",
"INFO root:test_capturing.py:12 sub2 logging",
"*== short test summary info ==*"
]
)

def test_caplog(self, testdir):
testdir.makepyfile(
"""
import logging

def test(subtests, caplog):
caplog.set_level(logging.INFO)
logging.info("start test")

with subtests.test("sub1"):
logging.info("inside %s", "subtest1")

assert len(caplog.records) == 2
assert caplog.records[0].getMessage() == "start test"
assert caplog.records[1].getMessage() == "inside subtest1"
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*1 passed*",
]
)


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

Expand Down