You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2022/02/10 13:46:29 UTC
[qpid-dispatch] 03/03: DISPATCH-2323 Add Pytest-compatible logging displayed on test failure
This is an automated email from the ASF dual-hosted git repository.
jdanek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git
commit 115aa6237e39e88c271cfcb2c9d1d39d614071d0
Author: Jiri Daněk <jd...@redhat.com>
AuthorDate: Sun Jan 30 18:04:12 2022 +0100
DISPATCH-2323 Add Pytest-compatible logging displayed on test failure
---
tests/system_test.py | 18 ++++++++++++------
tests/tox.ini.in | 3 +++
2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/tests/system_test.py b/tests/system_test.py
index 46a7eb5..432a0d2 100755
--- a/tests/system_test.py
+++ b/tests/system_test.py
@@ -28,7 +28,8 @@ Features:
- Sundry other tools.
"""
-from typing import Callable
+import logging
+from typing import Callable, List, Optional, Tuple
import errno
import sys
@@ -1406,19 +1407,22 @@ class Logger:
"""
Record an event log for a self test.
May print per-event or save events to be printed later.
+ Pytest will automatically collect the logs and will dump them for a failed test
Optional file opened in 'append' mode to which each log line is written.
"""
def __init__(self,
- title="Logger",
- print_to_console=False,
- save_for_dump=True,
- ofilename=None):
+ title: str = "Logger",
+ print_to_console: bool = False,
+ save_for_dump: bool = True,
+ python_log_level: Optional[int] = logging.DEBUG,
+ ofilename: Optional[str] = None) -> None:
self.title = title
self.print_to_console = print_to_console
self.save_for_dump = save_for_dump
- self.logs = []
+ self.python_log_level = python_log_level
self.ofilename = ofilename
+ self.logs: List[Tuple[Timestamp, str]] = []
def log(self, msg):
ts = Timestamp()
@@ -1427,6 +1431,8 @@ class Logger:
if self.print_to_console:
print("%s %s" % (ts, msg))
sys.stdout.flush()
+ if self.python_log_level is not None:
+ logging.log(self.python_log_level, f"{ts} {self.title}: {msg}")
if self.ofilename is not None:
with open(self.ofilename, 'a') as f_out:
f_out.write("%s %s\n" % (ts, msg))
diff --git a/tests/tox.ini.in b/tests/tox.ini.in
index 7f9c91c..96786c2 100644
--- a/tests/tox.ini.in
+++ b/tests/tox.ini.in
@@ -149,6 +149,9 @@ exclude =
# do not filter test file names
python_files = *.py
+# https://docs.pytest.org/en/6.2.x/logging.html#incompatible-changes-in-pytest-3-4
+log_level = DEBUG
+
# pylint
[MESSAGE_CONTROL]
disable =
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org