Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False assertion when using Qt QStateMachine does not cause test to fail #13245

Closed
nocro opened this issue Feb 21, 2025 · 2 comments
Closed

False assertion when using Qt QStateMachine does not cause test to fail #13245

nocro opened this issue Feb 21, 2025 · 2 comments

Comments

@nocro
Copy link

nocro commented Feb 21, 2025

Hi,
I'm doing a test with a Qt StateMachine. The Qt StateMachine do an assert False but the test do not fail.
The fail is catched by sys.excepthook if we setup it and I could check that pytest saw the error in the end with:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
    outcome = yield
    excinfo = outcome.excinfo
    if excinfo:
        pytest.fail(f"Exception catched: {excinfo.value}", pytrace=True)

I created a minimal exemple to reproduce it:

import os
import sys
import pytest


from PyQt5 import QtCore, QtWidgets, QtGui, QtTest


@pytest.fixture(scope='session')
def qapp():
    global app
    app = QtWidgets.QApplication([])
    yield app

    app.quit()

def exception_hook(type, value: BaseException, tb) -> None:
    print("Intercepted by sys.excepthook :", value)
    print(os.getpid())
    pytest.fail('should fail')


def test_error(qapp) -> None:
    sys.excepthook = exception_hook # type: ignore
    class FailingState(QtCore.QState):
        def onEntry(self, event: QtCore.QEvent) -> None:
            assert False

        def onExit(self, event: QtCore.QEvent) -> None:
            assert False


    class Container(QtCore.QObject):
        signal = QtCore.pyqtSignal()

        def __init__(self):
            super().__init__()
            self.machine = QtCore.QStateMachine()
            base_state = QtCore.QState(self.machine)
            failing_state = FailingState(self.machine)

            self.machine.setInitialState(failing_state)

            transition = QtCore.QSignalTransition(self.signal)
            transition.setTargetState(failing_state)
            base_state.addTransition(transition)

            self.machine.start()

    cont = Container()
    QtTest.QTest.qWait(500)
    cont.signal.emit()
    QtTest.QTest.qWait(500)

Here the pip list of the python I'm using:
pip_list.txt
I'm working on Centos 7

Thanks,
Simon

@RonnyPfannschmidt
Copy link
Member

Legacy hookwrappers don't support exceptions being raised out of them

Use outcome.set_exception or migrate to modern wrappers

@nocro
Copy link
Author

nocro commented Feb 21, 2025

Merci

@nocro nocro closed this as completed Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants