Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pytestqt/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
41 changes: 40 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
pytest_plugins = 'pytester'
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()
7 changes: 7 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

"""
Expand Down
41 changes: 0 additions & 41 deletions tests/test_wait_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -309,9 +274,3 @@ class Obj(QtCore.QObject):

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