From 7e8974c1c35a106070557a9b2d96638bb0704333 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 30 Oct 2015 18:45:21 -0200 Subject: [PATCH 1/4] RuntimeError when failing to import any Qt binding Fix #109 --- pytestqt/qt_compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytestqt/qt_compat.py b/pytestqt/qt_compat.py index 896b67d6..55a0a6cb 100644 --- a/pytestqt/qt_compat.py +++ b/pytestqt/qt_compat.py @@ -33,7 +33,7 @@ def _guess_qt_api(): return 'pyqt5' else: msg = 'pytest-qt requires either PySide, PyQt4 or PyQt5 to be installed' - raise ImportError(msg) + raise RuntimeError(msg) # backward compatibility support: PYTEST_QT_FORCE_PYQT if os.environ.get('PYTEST_QT_FORCE_PYQT', 'false') == 'true': From ef3f694e8d502fa6c2a69243cfef945028cbb27b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 30 Oct 2015 19:05:57 -0200 Subject: [PATCH 2/4] Add note to changelog related to #109 --- CHANGELOG.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f818e6c0..1dc710b9 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,10 +11,16 @@ - ``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`_)! + +- raise ``RuntimeError`` instead of ``ImportError`` when failing to import + any Qt binding: raising the latter causes `pluggy` in `pytest-2.8` to + generate a subtle warning instead of a full blown error. + Thanks `@Sheeo`_ for bringing this problem to attention (closes `109`_). .. _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 +.. _109: https://github.com/pytest-dev/pytest-qt/issues/109 1.8.0 @@ -264,3 +270,4 @@ First working version. .. _@fabioz: https://github.com/fabioz .. _@baudren: https://github.com/baudren .. _@itghisi: https://github.com/itghisi +.. _@Sheeo: https://github.com/Sheeo From a05e325582d7e9a2674b95f0fa3ff4017ae512a1 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 30 Oct 2015 21:46:37 -0200 Subject: [PATCH 3/4] Increase wait() to account for slow slaves on Windows Somehow only 100ms was not enough for this test to pass reliably on Windows --- tests/test_wait_signal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_wait_signal.py b/tests/test_wait_signal.py index cc36d8f6..46a0ab92 100644 --- a/tests/test_wait_signal.py +++ b/tests/test_wait_signal.py @@ -312,6 +312,6 @@ class Obj(QtCore.QObject): def test_qtbot_wait(qtbot, stop_watch): stop_watch.start() - qtbot.wait(100) + qtbot.wait(250) stop_watch.stop() - assert stop_watch.elapsed >= 95 + assert stop_watch.elapsed >= 220 From f2bcc5c47533dd82b0c9a5d05a5124dbc649cf64 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 30 Oct 2015 21:48:23 -0200 Subject: [PATCH 4/4] Move test_qtbot_wait to test_basics and stop_watch fixture to conftest --- tests/conftest.py | 41 ++++++++++++++++++++++++++++++++++++++- tests/test_basics.py | 7 +++++++ tests/test_wait_signal.py | 41 --------------------------------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 129fb68b..9a35a0c8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,40 @@ -pytest_plugins = 'pytester' \ No newline at end of file +import pytest +import time +import sys + +pytest_plugins = 'pytester' + + +@pytest.fixture +def stop_watch(): + """ + Fixture that makes it easier for tests to ensure signals emitted and + timeouts are being respected. + """ + # time.clock() is more accurate on Windows + get_time = time.clock if sys.platform.startswith('win') else time.time + + 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,)) + assert self.elapsed < max_wait_ms + + return StopWatch() diff --git a/tests/test_basics.py b/tests/test_basics.py index 1e4a55ea..10e130a3 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -204,6 +204,13 @@ def test_foo(widget): ]) +def test_qtbot_wait(qtbot, stop_watch): + stop_watch.start() + qtbot.wait(250) + stop_watch.stop() + assert stop_watch.elapsed >= 220 + + class EventRecorder(QWidget): """ diff --git a/tests/test_wait_signal.py b/tests/test_wait_signal.py index 46a0ab92..e995cf3b 100644 --- a/tests/test_wait_signal.py +++ b/tests/test_wait_signal.py @@ -211,41 +211,6 @@ def _emit(self, signal): timer.shutdown() -@pytest.fixture -def stop_watch(): - """ - Fixture that makes it easier for tests to ensure signals emitted and - timeouts are being respected in waitSignal and waitSignals tests. - """ - # time.clock() is more accurate on Windows - get_time = time.clock if sys.platform.startswith('win') else time.time - - 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,)) - assert self.elapsed < max_wait_ms - - return StopWatch() - - @pytest.mark.parametrize('multiple', [True, False]) @pytest.mark.parametrize('raising', [True, False]) def test_wait_signals_handles_exceptions(qtbot, multiple, raising, signaller): @@ -309,9 +274,3 @@ class Obj(QtCore.QObject): assert sip.isdeleted(obj) - -def test_qtbot_wait(qtbot, stop_watch): - stop_watch.start() - qtbot.wait(250) - stop_watch.stop() - assert stop_watch.elapsed >= 220