From c62c453fce65ce62743e3b2d69bdc84cc4140464 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Oct 2015 17:48:33 +0100 Subject: [PATCH] Add a qtbot.wait method. Closes #107. --- CHANGELOG.rst | 5 +++++ pytestqt/qtbot.py | 12 ++++++++++++ tests/test_wait_signal.py | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index af1f782d..f818e6c0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,8 +8,13 @@ - Widgets registered by ``qtbot.addWidget`` are now closed before all other fixtures are tear down (`106`_). Thanks `@The-Compiler`_ for request. +- ``qtbot`` now has a new ``wait`` method which does a blocking wait while the + event loop continues to run, similar to ``QTest::qWait``. Thanks + `@The-Compiler`_ for the PR (closes `107`_)! + .. _105: https://github.com/pytest-dev/pytest-qt/issues/105 .. _106: https://github.com/pytest-dev/pytest-qt/issues/106 +.. _107: https://github.com/pytest-dev/pytest-qt/issues/107 1.8.0 diff --git a/pytestqt/qtbot.py b/pytestqt/qtbot.py index fce1de6c..88649131 100644 --- a/pytestqt/qtbot.py +++ b/pytestqt/qtbot.py @@ -311,6 +311,18 @@ def waitSignals(self, signals=None, timeout=1000, raising=False): wait_signals = waitSignals # pep-8 alias + def wait(self, ms): + """ + .. versionadded:: 1.9 + + Waits for ``ms`` milliseconds. + + While waiting, events will be processed and your test will stay + responsive to user interface events or network communication. + """ + blocker = MultiSignalBlocker(timeout=ms) + blocker.wait() + # provide easy access to SignalTimeoutError to qtbot fixtures QtBot.SignalTimeoutError = SignalTimeoutError diff --git a/tests/test_wait_signal.py b/tests/test_wait_signal.py index 4cdfda2e..cc36d8f6 100644 --- a/tests/test_wait_signal.py +++ b/tests/test_wait_signal.py @@ -224,20 +224,24 @@ class StopWatch: def __init__(self): self._start_time = None + self.elapsed = None def start(self): self._start_time = get_time() + def stop(self): + self.elapsed = (get_time() - self._start_time) * 1000.0 + def check(self, timeout, *delays): """ Make sure either timeout (if given) or at most of the given delays used to trigger a signal has passed. """ + self.stop() if timeout is None: timeout = max(delays) * 1.35 # 35% tolerance max_wait_ms = max(delays + (timeout,)) - elapsed_ms = (get_time() - self._start_time) * 1000.0 - assert elapsed_ms < max_wait_ms + assert self.elapsed < max_wait_ms return StopWatch() @@ -304,3 +308,10 @@ class Obj(QtCore.QObject): obj.deleteLater() assert sip.isdeleted(obj) + + +def test_qtbot_wait(qtbot, stop_watch): + stop_watch.start() + qtbot.wait(100) + stop_watch.stop() + assert stop_watch.elapsed >= 95