Skip to content

qtbot.waitCallback #156

@The-Compiler

Description

@The-Compiler

I think we talked about this but never really opened an issue.

I generalized by code in qutebrowser a bit today:

class CallbackChecker(QObject):

    """Check if a value provided by a callback is the expected one."""

    got_result = pyqtSignal(object)
    UNSET = object()

    def __init__(self, qtbot, parent=None):
        super().__init__(parent)
        self._qtbot = qtbot
        self._result = self.UNSET

    def callback(self, result):
        self._result = result
        self.got_result.emit(result)

    def check(self, expected):
        if self._result is self.UNSET:
            with self._qtbot.waitSignal(self.got_result):
                pass
        assert self._result == expected


@pytest.fixture
def callback_checker(qtbot):
    return CallbackChecker(qtbot)

Example usage:

def test_foo(callback_checker, webengineview)
    expected = 2
    webengineview.page().runJavaScript('1 + 1', callback_checker.callback)
    callback_checker.check(expected)

Next step would be to add this to pytest-qt, which I'll hopefully do soon™

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions