Skip to content

Add log capture assertions #1962

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions python/tests/unit/common/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
import pytest


def test_basics():
def assert_logged(capsys, *messages: str) -> None:
"""Read captured logs and ensure *messages* appear in the output."""
captured = capsys.readouterr()
output = captured.out + captured.err
for msg in messages:
assert msg in output, f"{msg!r} not found in captured output"


def test_basics(capsys):
Comment on lines +5 to +13
Copy link
Preview

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using pytest's caplog fixture instead of capsys for logging assertions, which provides richer log record inspection and severity filtering.

Suggested change
def assert_logged(capsys, *messages: str) -> None:
"""Read captured logs and ensure *messages* appear in the output."""
captured = capsys.readouterr()
output = captured.out + captured.err
for msg in messages:
assert msg in output, f"{msg!r} not found in captured output"
def test_basics(capsys):
def assert_logged(caplog, *messages: str) -> None:
"""Ensure *messages* appear in the captured logs."""
log_output = " ".join(record.message for record in caplog.records)
for msg in messages:
assert msg in log_output, f"{msg!r} not found in captured logs"
def test_basics(caplog):

Copilot uses AI. Check for mistakes.

dart.common.trace("trace log")
dart.common.debug("debug log")
dart.common.info("info log")
dart.common.warn("warn log")
dart.common.error("error log")
dart.common.fatal("fatal log")

assert_logged(
capsys,
"trace log",
"debug log",
"info log",
"warn log",
"error log",
"fatal log",
)

def test_arguments():

def test_arguments(capsys):
val = 10
dart.common.info("Log with param '{}' and '{}'".format(1, val))

assert_logged(capsys, "Log with param '1' and '10'")


if __name__ == "__main__":
pytest.main()
Loading