tests: Optionally skip a test when waiting for log.

This commit is contained in:
Florian Bruhin 2016-01-06 08:22:30 +01:00
parent 6a97e98007
commit 29dd6af976
3 changed files with 25 additions and 7 deletions

View File

@ -32,6 +32,7 @@ import pytest_bdd as bdd
from PyQt5.QtCore import QElapsedTimer
from PyQt5.QtGui import QClipboard
from integration import testprocess
from helpers import utils
@ -142,8 +143,9 @@ def wait_until_loaded(quteproc, path):
@bdd.when(bdd.parsers.re(r'I wait for (?P<is_regex>regex )?"'
r'(?P<pattern>[^"]+)" in the log'))
def wait_in_log(quteproc, is_regex, pattern):
r'(?P<pattern>[^"]+)" in the log(?P<do_skip> or skip '
r'the test)?'))
def wait_in_log(quteproc, is_regex, pattern, do_skip):
"""Wait for a given pattern in the qutebrowser log.
If used like "When I wait for regex ... in the log" the argument is treated
@ -151,7 +153,8 @@ def wait_in_log(quteproc, is_regex, pattern):
"""
if is_regex:
pattern = re.compile(pattern)
quteproc.wait_for(message=pattern)
quteproc.wait_for(message=pattern, do_skip=bool(do_skip))
@bdd.when(bdd.parsers.re(r'I wait for the (?P<category>error|message|warning) '

View File

@ -144,6 +144,13 @@ class TestWaitFor:
with pytest.raises(TypeError):
pyproc.wait_for()
def test_do_skip(self, pyproc):
"""Test wait_for when getting no text at all, with do_skip."""
pyproc.code = "pass"
pyproc.start()
with pytest.raises(pytest.skip.Exception):
pyproc.wait_for(data="foobar", timeout=100, do_skip=True)
class TestEnsureNotLogged:

View File

@ -287,7 +287,8 @@ class Process(QObject):
return line
return None
def wait_for(self, timeout=None, *, override_waited_for=False, **kwargs):
def wait_for(self, timeout=None, *, override_waited_for=False,
do_skip=False, **kwargs):
"""Wait until a given value is found in the data.
Keyword arguments to this function get interpreted as attributes of the
@ -298,13 +299,16 @@ class Process(QObject):
timeout: How long to wait for the message.
override_waited_for: If set, gets triggered by previous messages
again.
do_skip: If set, call pytest.skip on a timeout.
Return:
The matched line.
"""
__tracebackhide__ = True
if timeout is None:
if 'CI' in os.environ:
if do_skip:
timeout = 2000
elif 'CI' in os.environ:
timeout = 15000
else:
timeout = 5000
@ -326,8 +330,12 @@ class Process(QObject):
while True:
got_signal = spy.wait(timeout)
if not got_signal or elapsed_timer.hasExpired(timeout):
raise WaitForTimeout("Timed out after {}ms waiting for "
"{!r}.".format(timeout, kwargs))
msg = "Timed out after {}ms waiting for {!r}.".format(
timeout, kwargs)
if do_skip:
pytest.skip(msg)
else:
raise WaitForTimeout(msg)
for args in spy:
assert len(args) == 1