1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
import pytest
def test_wait_until(qtbot, wait_4_ticks_callback, tick_counter):
tick_counter.start(100)
qtbot.waitUntil(wait_4_ticks_callback, timeout=1000)
assert tick_counter.ticks >= 4
def test_wait_until_timeout(qtbot, wait_4_ticks_callback, tick_counter):
tick_counter.start(200)
with pytest.raises(qtbot.TimeoutError):
qtbot.waitUntil(wait_4_ticks_callback, timeout=100)
assert tick_counter.ticks < 4
def test_invalid_callback_return_value(qtbot):
with pytest.raises(ValueError):
qtbot.waitUntil(lambda: [])
def test_pep8_alias(qtbot):
qtbot.wait_until
@pytest.fixture(params=["predicate", "assert"])
def wait_4_ticks_callback(request, tick_counter):
"""Parametrized fixture which returns the two possible callback methods that can be
passed to ``waitUntil``: predicate and assertion.
"""
if request.param == "predicate":
return lambda: tick_counter.ticks >= 4
else:
def check_ticks():
assert tick_counter.ticks >= 4
return check_ticks
@pytest.fixture
def tick_counter():
"""
Returns an object which counts timer "ticks" periodically.
"""
from pytestqt.qt_compat import qt_api
class Counter:
def __init__(self):
self._ticks = 0
self.timer = qt_api.QtCore.QTimer()
self.timer.timeout.connect(self._tick)
def start(self, ms):
self.timer.start(ms)
def _tick(self):
self._ticks += 1
@property
def ticks(self):
return self._ticks
counter = Counter()
yield counter
counter.timer.stop()
|