Skip to content

Commit aa2ee36

Browse files
committed
Refactor Message into Record
Reuse same ideas and terminology from `logging` when possible
1 parent b6ce650 commit aa2ee36

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ def test_qtlog_fixture(qtlog):
5555
qDebug('this is a DEBUG message')
5656
qWarning('this is a WARNING message')
5757
qCritical('this is a CRITICAL message')
58-
messages = [(m.type, m.message.strip()) for m in qtlog.messages]
59-
assert messages == [
58+
records = [(m.type, m.message.strip()) for m in qtlog.records]
59+
assert records == [
6060
(QtDebugMsg, 'this is a DEBUG message'),
6161
(QtWarningMsg, 'this is a WARNING message'),
6262
(QtCriticalMsg, 'this is a CRITICAL message'),
6363
]
64-
# `messages` attribute is read-only
64+
# `records` attribute is read-only
6565
with pytest.raises(AttributeError):
66-
qtlog.messages = []
66+
qtlog.records = []
6767

6868

6969
def test_fixture_with_logging_disabled(testdir):
@@ -79,7 +79,7 @@ def test_fixture_with_logging_disabled(testdir):
7979
8080
def test_types(qtlog):
8181
qWarning('message')
82-
assert qtlog.messages == []
82+
assert qtlog.records == []
8383
"""
8484
)
8585
res = testdir.runpytest('--no-qt-log')

pytestqt/plugin.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,15 @@ def pytest_report_header():
434434
return ['qt-api: %s' % QT_API]
435435

436436

437+
@pytest.fixture
438+
def qtlog(request):
439+
"""Fixture that can access messages captured during testing"""
440+
if hasattr(request._pyfuncitem, 'qt_log_capture'):
441+
return request._pyfuncitem.qt_log_capture
442+
else:
443+
return _QtMessageCapture()
444+
445+
437446
class QtLoggingPlugin(object):
438447
"""
439448
Pluging responsible for installing a QtMessageHandler before each
@@ -461,12 +470,12 @@ def pytest_runtest_makereport(self, item, call):
461470
long_repr = getattr(report, 'longrepr', None)
462471
if hasattr(long_repr, 'addsection'):
463472
lines = []
464-
for m in item.qt_log_capture.messages:
465-
lines.append('{m.type_name}: {m.message}'.format(m=m))
473+
for r in item.qt_log_capture.records:
474+
lines.append('{r.type_name}: {r.message}'.format(r=r))
466475
if lines:
467476
long_repr.addsection('Captured Qt messages',
468477
'\n'.join(lines))
469-
# Release the handler resources.
478+
470479
qInstallMsgHandler(item.qt_previous_handler)
471480
del item.qt_previous_handler
472481
del item.qt_log_capture
@@ -477,11 +486,11 @@ class _QtMessageCapture(object):
477486
Captures Qt messages when its `handle` method is installed using
478487
qInstallMsgHandler, and stores them into `messages` attribute.
479488
480-
:attr messages: list of Message named-tuples.
489+
:attr messages: list of Record named-tuples.
481490
"""
482491

483492
def __init__(self):
484-
self._messages = []
493+
self._records = []
485494

486495
def _handle(self, msg_type, message):
487496
"""
@@ -490,23 +499,33 @@ def _handle(self, msg_type, message):
490499
"""
491500
if isinstance(message, bytes):
492501
message = message.decode('utf-8', errors='replace')
493-
self._messages.append(Message(msg_type, message))
502+
self._records.append(Record(msg_type, message))
494503

495504
@property
496-
def messages(self):
505+
def records(self):
497506
"""Access messages captured so far.
498507
499-
:rtype: list of `Message` namedtuples.
508+
:rtype: list of `Record` instances.
500509
"""
501-
return self._messages[:]
510+
return self._records[:]
511+
502512

513+
class Record(object):
514+
"""Hold information about a message sent by one of Qt log functions.
503515
504-
class Message(object):
516+
:attr str message: message contents.
517+
:attr Qt.QtMsgType type: enum that identifies message type
518+
:attr str type_name: `type` as a string
519+
:attr str log_type_name:
520+
type name similar to the logging package, for example ``DEBUG``,
521+
``WARNING``, etc.
522+
"""
505523

506524
def __init__(self, msg_type, message):
507525
self._type = msg_type
508526
self._message = message
509527
self._type_name = self._get_msg_type_name(msg_type)
528+
self._log_type_name = self._get_log_type_name(msg_type)
510529

511530
@property
512531
def message(self):
@@ -520,6 +539,10 @@ def type(self):
520539
def type_name(self):
521540
return self._type_name
522541

542+
@property
543+
def log_type_name(self):
544+
return self._log_type_name
545+
523546
@classmethod
524547
def _get_msg_type_name(cls, msg_type):
525548
"""
@@ -535,11 +558,17 @@ def _get_msg_type_name(cls, msg_type):
535558
}
536559
return cls._type_name_map[msg_type]
537560

538-
539-
@pytest.fixture
540-
def qtlog(request):
541-
"""Fixture that can access messages captured during testing"""
542-
if hasattr(request._pyfuncitem, 'qt_log_capture'):
543-
return request._pyfuncitem.qt_log_capture
544-
else:
545-
return _QtMessageCapture()
561+
@classmethod
562+
def _get_log_type_name(cls, msg_type):
563+
"""
564+
Return a string representation of the given QtMsgType enum
565+
value in the same style used by the builtin logging package.
566+
"""
567+
if not getattr(cls, '_log_type_name_map', None):
568+
cls._log_type_name_map = {
569+
QtDebugMsg: 'DEBUG',
570+
QtWarningMsg: 'WARNING',
571+
QtCriticalMsg: 'CRITICAL',
572+
QtFatalMsg: 'FATAL',
573+
}
574+
return cls._log_type_name_map[msg_type]

0 commit comments

Comments
 (0)