0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

fix: mock QTimer, improve timeout message

This commit is contained in:
2025-02-06 14:37:03 +01:00
parent 8aba3d975f
commit fb051865d5
3 changed files with 64 additions and 26 deletions

View File

@ -2,7 +2,6 @@ from unittest import mock
import pytest
from pytestqt.exceptions import TimeoutError as QtBotTimeoutError
from qtpy.QtCore import QTimer
from qtpy.QtWidgets import QApplication
from bec_widgets.cli.rpc.rpc_register import RPCRegister
@ -10,22 +9,6 @@ from bec_widgets.qt_utils import error_popups
from bec_widgets.utils import bec_dispatcher as bec_dispatcher_module
class TestableQTimer(QTimer):
_instances = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
TestableQTimer._instances.append(self)
@classmethod
def check_all_stopped(cls, qtbot):
try:
qtbot.waitUntil(lambda: all(not timer.isActive() for timer in cls._instances))
except QtBotTimeoutError as exc:
raise TimeoutError("Failed to stop all timers") from exc
cls._instances = []
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
@ -36,17 +19,16 @@ def pytest_runtest_makereport(item, call):
@pytest.fixture(autouse=True)
def qapplication(qtbot, request): # pylint: disable=unused-argument
with mock.patch("qtpy.QtCore.QTimer", new=TestableQTimer):
yield
def qapplication(qtbot, request, testable_qtimer_class): # pylint: disable=unused-argument
yield
# if the test failed, we don't want to check for open widgets as
# it simply pollutes the output
if request.node.stash._storage.get("failed"):
print("Test failed, skipping cleanup checks")
return
# if the test failed, we don't want to check for open widgets as
# it simply pollutes the output
if request.node.stash._storage.get("failed"):
print("Test failed, skipping cleanup checks")
return
TestableQTimer.check_all_stopped(qtbot)
testable_qtimer_class.check_all_stopped(qtbot)
qapp = QApplication.instance()
qapp.processEvents()