Skip to content

Commit 374dbeb

Browse files
committed
Add qtlog fixture for logging capture
1 parent c6495cf commit 374dbeb

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
22

3+
from pytestqt.qt_compat import qDebug, qWarning, qCritical, QtDebugMsg, \
4+
QtWarningMsg, QtCriticalMsg
5+
6+
37
pytest_plugins = 'pytester'
48

59

@@ -8,11 +12,13 @@
812
(False, True)])
913
def test_basic_logging(testdir, test_succeds, qt_log):
1014
"""
15+
Test Qt logging capture output.
16+
1117
:type testdir: _pytest.pytester.TmpTestdir
1218
"""
1319
testdir.makepyfile(
1420
"""
15-
from pytestqt.qt_compat import qDebug, qWarning, qCritical, qFatal
21+
from pytestqt.qt_compat import qDebug, qWarning, qCritical
1622
1723
def test_types():
1824
qDebug('this is a DEBUG message')
@@ -40,3 +46,21 @@ def test_types():
4046
'this is a WARNING message*',
4147
'this is a CRITICAL message*',
4248
])
49+
50+
51+
def test_qtlog_fixture(qtlog):
52+
"""
53+
Test qtlog fixture.
54+
"""
55+
qDebug('this is a DEBUG message')
56+
qWarning('this is a WARNING message')
57+
qCritical('this is a CRITICAL message')
58+
# note that in the messages below, we have an extra space at the end
59+
# added by Qt
60+
assert qtlog.messages == [
61+
(QtDebugMsg, 'this is a DEBUG message '),
62+
(QtWarningMsg, 'this is a WARNING message '),
63+
(QtCriticalMsg, 'this is a CRITICAL message '),
64+
]
65+
with pytest.raises(AttributeError):
66+
qtlog.messages = []

pytestqt/plugin.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ def __init__(self, config):
444444
self.config = config
445445

446446
def pytest_runtest_setup(self, item):
447-
item.qt_log_handler = _QtMessageCapture()
448-
previous_handler = qInstallMsgHandler(item.qt_log_handler.handle)
447+
item.qt_log_capture = _QtMessageCapture()
448+
previous_handler = qInstallMsgHandler(item.qt_log_capture._handle)
449449
item.qt_previous_handler = previous_handler
450450

451451
@pytest.mark.hookwrapper
@@ -461,7 +461,7 @@ def pytest_runtest_makereport(self, item, call):
461461
long_repr = getattr(report, 'longrepr', None)
462462
if hasattr(long_repr, 'addsection'):
463463
lines = []
464-
for msg_type, msg in item.qt_log_handler.messages:
464+
for msg_type, msg in item.qt_log_capture.messages:
465465
msg_name = _QtMessageCapture.get_msg_name(msg_type)
466466
lines.append('{0}: {1}'.format(msg_name, msg))
467467
if lines:
@@ -470,7 +470,7 @@ def pytest_runtest_makereport(self, item, call):
470470
# Release the handler resources.
471471
qInstallMsgHandler(item.qt_previous_handler)
472472
del item.qt_previous_handler
473-
del item.qt_log_handler
473+
del item.qt_log_capture
474474

475475

476476
class _QtMessageCapture(object):
@@ -484,23 +484,37 @@ class _QtMessageCapture(object):
484484
Message = collections.namedtuple('Message', 'msg_type, msg')
485485

486486
def __init__(self):
487-
self.messages = []
487+
self._messages = []
488488

489-
def handle(self, msg_type, msg):
489+
def _handle(self, msg_type, msg):
490490
"""
491491
Method to be installed using qInstallMsgHandler, stores each message
492492
into the `messages` attribute.
493493
"""
494494
if isinstance(msg, bytes):
495495
msg = msg.decode('utf-8', errors='replace')
496-
self.messages.append(self.Message(msg_type, msg))
496+
self._messages.append(self.Message(msg_type, msg))
497+
498+
@property
499+
def messages(self):
500+
"""Acess messages captured so far.
501+
502+
:rtype: list of `Message` namedtuples.
503+
"""
504+
return self._messages[:]
497505

498506
@classmethod
499507
def get_msg_name(cls, msg_type):
500-
"""Returns a string representation of the given QtMsgType enum value."""
508+
"""Return a string representation of the given QtMsgType enum value."""
501509
return {
502510
QtDebugMsg: 'QtDebugMsg',
503511
QtWarningMsg: 'QtWarningMsg',
504512
QtCriticalMsg: 'QtCriticalMsg',
505513
QtFatalMsg: 'QtFatalMsg',
506514
}[msg_type]
515+
516+
517+
@pytest.fixture
518+
def qtlog(request):
519+
"""Fixture that can access messages captured during testing"""
520+
return request._pyfuncitem.qt_log_capture

0 commit comments

Comments
 (0)