@@ -434,6 +434,15 @@ def pytest_report_header():
434
434
return ['qt-api: %s' % QT_API ]
435
435
436
436
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
+
437
446
class QtLoggingPlugin (object ):
438
447
"""
439
448
Pluging responsible for installing a QtMessageHandler before each
@@ -461,12 +470,12 @@ def pytest_runtest_makereport(self, item, call):
461
470
long_repr = getattr (report , 'longrepr' , None )
462
471
if hasattr (long_repr , 'addsection' ):
463
472
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 ))
466
475
if lines :
467
476
long_repr .addsection ('Captured Qt messages' ,
468
477
'\n ' .join (lines ))
469
- # Release the handler resources.
478
+
470
479
qInstallMsgHandler (item .qt_previous_handler )
471
480
del item .qt_previous_handler
472
481
del item .qt_log_capture
@@ -477,11 +486,11 @@ class _QtMessageCapture(object):
477
486
Captures Qt messages when its `handle` method is installed using
478
487
qInstallMsgHandler, and stores them into `messages` attribute.
479
488
480
- :attr messages: list of Message named-tuples.
489
+ :attr messages: list of Record named-tuples.
481
490
"""
482
491
483
492
def __init__ (self ):
484
- self ._messages = []
493
+ self ._records = []
485
494
486
495
def _handle (self , msg_type , message ):
487
496
"""
@@ -490,23 +499,33 @@ def _handle(self, msg_type, message):
490
499
"""
491
500
if isinstance (message , bytes ):
492
501
message = message .decode ('utf-8' , errors = 'replace' )
493
- self ._messages .append (Message (msg_type , message ))
502
+ self ._records .append (Record (msg_type , message ))
494
503
495
504
@property
496
- def messages (self ):
505
+ def records (self ):
497
506
"""Access messages captured so far.
498
507
499
- :rtype: list of `Message` namedtuples .
508
+ :rtype: list of `Record` instances .
500
509
"""
501
- return self ._messages [:]
510
+ return self ._records [:]
511
+
502
512
513
+ class Record (object ):
514
+ """Hold information about a message sent by one of Qt log functions.
503
515
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
+ """
505
523
506
524
def __init__ (self , msg_type , message ):
507
525
self ._type = msg_type
508
526
self ._message = message
509
527
self ._type_name = self ._get_msg_type_name (msg_type )
528
+ self ._log_type_name = self ._get_log_type_name (msg_type )
510
529
511
530
@property
512
531
def message (self ):
@@ -520,6 +539,10 @@ def type(self):
520
539
def type_name (self ):
521
540
return self ._type_name
522
541
542
+ @property
543
+ def log_type_name (self ):
544
+ return self ._log_type_name
545
+
523
546
@classmethod
524
547
def _get_msg_type_name (cls , msg_type ):
525
548
"""
@@ -535,11 +558,17 @@ def _get_msg_type_name(cls, msg_type):
535
558
}
536
559
return cls ._type_name_map [msg_type ]
537
560
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