Skip to content

Logging capture causes test errors #225

@wshanks

Description

@wshanks

When I run a test using the qtbot fixture with PySide2, I get the following error

============================= test session starts ==============================
platform linux -- Python 3.7.0, pytest-3.7.3, py-1.5.4, pluggy-0.7.1
PySide2 5.11.1 -- Qt runtime 5.11.1 -- Qt compiled 5.11.1
rootdir: /home/user/Documents/Writing/Coding/Python/myproject, inifile:
plugins: qt-3.0.0, pudb-0.6, cov-2.5.1
collected 1 item

test_pytestqt.py E                                                       [100%]

==================================== ERRORS ====================================
_____________________ ERROR at setup of test_preselection ______________________

qapp_args = []

    @pytest.yield_fixture(scope='session')
    def qapp(qapp_args):
        """
        Fixture that instantiates the QApplication instance that will be used by
        the tests.
    
        You can use the ``qapp`` fixture in tests which require a ``QApplication``
        to run, but where you don't need full ``qtbot`` functionality.
        """
        app = qt_api.QApplication.instance()
        if app is None:
            global _qapp_instance
>           _qapp_instance = qt_api.QApplication(qapp_args)

/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/plugin.py:50: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py:186: in _handle_with_context
    self._append_new_record(msg_type, message, context=context)
/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py:172: in _append_new_record
    self._records.append(Record(msg_type, message, ignored, context))
/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py:217: in __init__
    self._type_name = self._get_msg_type_name(msg_type)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'pytestqt.logging.Record'>
msg_type = PySide2.QtCore.QtMsgType.QtInfoMsg

    @classmethod
    def _get_msg_type_name(cls, msg_type):
        """
            Return a string representation of the given QtMsgType enum
            value.
            """
        if not getattr(cls, '_type_name_map', None):
            cls._type_name_map = {
                qt_api.QtDebugMsg: 'QtDebugMsg',
                qt_api.QtWarningMsg: 'QtWarningMsg',
                qt_api.QtCriticalMsg: 'QtCriticalMsg',
                qt_api.QtFatalMsg: 'QtFatalMsg',
            }
>       return cls._type_name_map[msg_type]
E       KeyError: PySide2.QtCore.QtMsgType.QtInfoMsg

/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py:244: KeyError
=========================== 1 error in 0.17 seconds ============================

I get this error even for a test module with these contents:

def test_preselection(qtbot):
    pass

The output of pip freeze is

astroid==2.0.4                                                                                                                                                                                                   
atomicwrites==1.2.0                                                                                                                                                                                               
attrs==18.1.0                                                                                                                                                                                        
coverage==4.5.1                                                                                                                                                                                                   
isort==4.3.4                                                                                                                                                                                                      
lazy-object-proxy==1.3.1                                                                                                                                                          
mccabe==0.6.1                                                                                                                                                                                                     
more-itertools==4.3.0                          
mypy==0.620                                                                                                                                                                                                        
pluggy==0.7.1                                                                                                                                                                                                      
pudb==2018.1                                                                                                                                                                                                       
py==1.5.4                                                                                                
pycodestyle==2.4.0                                                                                                                                                                                                
Pygments==2.2.0                                                                                                                                                                                                    
pylint==2.1.1                                                                                                                                                                                                     
PySide2==5.11.1                                                                                                                                                                                                   
pytest==3.7.3                                                                                                                                                                                                     
pytest-cov==2.5.1                                                                                                                                                                                                 
pytest-pudb==0.6                                                                                                                                                                                                   
pytest-qt==3.0.0            

Switching from PySide2 to PyQt5, I get a different error message:

===================================================================================================== ERRORS ======================================================================================================
_______________________________________________________________________________________ ERROR at setup of test_preselection _______________________________________________________________________________________
SETUP ERROR: Qt exceptions in virtual methods:
________________________________________________________________________________
Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 186, in _handle_with_context                                                           
    self._append_new_record(msg_type, message, context=context)
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 172, in _append_new_record                                                             
    self._records.append(Record(msg_type, message, ignored, context))
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 217, in __init__                                                                       
    self._type_name = self._get_msg_type_name(msg_type)
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 244, in _get_msg_type_name                                                             
    return cls._type_name_map[msg_type]
KeyError: 4
________________________________________________________________________________

---------------------------------------------------------------------------------------------- Captured stderr setup ----------------------------------------------------------------------------------------------
Qt exceptions in virtual methods:
________________________________________________________________________________
Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 186, in _handle_with_context                                                           
    self._append_new_record(msg_type, message, context=context)
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 172, in _append_new_record                                                             
    self._records.append(Record(msg_type, message, ignored, context))
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 217, in __init__                                                                       
    self._type_name = self._get_msg_type_name(msg_type)
  File "/home/user/.local/share/virtualenvs/myproject-TS3ABiG4/lib/python3.7/site-packages/pytestqt/logging.py", line 244, in _get_msg_type_name                                                             
    return cls._type_name_map[msg_type]
KeyError: 4

For both PySide2 and PyQt, the test runs successfully when using pytest --no-qt-log.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions