question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging capture causes test errors

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
wshankscommented, Sep 1, 2018

Okay, I see. For the record, here is the warning: Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.

1reaction
wshankscommented, Aug 31, 2018

Works for me in PySide2 and PyQt5, thanks!

By the way, why couldn’t you reproduce the issue? Because you have an older version of Qt? My testing was on Arch Linux.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way for pytest to check if a log entry was made at ...
Uncommenting the assert statement fails the test without errors, and prints [] to stdout, and log critical to stderr.
Read more >
Exceptional Logging of Exceptions in Python
Aaron Maxwell shares 6 Python exception logging patterns that show why logging is one of developers' most powerful tools for dealing with ...
Read more >
How to manage logging
pytest captures log messages of level WARNING or above automatically and displays them in their own section for each failed test in the...
Read more >
Optimal Logging
Repetitive errors - Do not repetitively log the same or similar error. This can quickly fill a log and hide the actual cause....
Read more >
Logcapture: capture logging during tests
When an error or failure occurs, the captured log messages are attached to the running test in the test.capturedLogging attribute, and displayed with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found