Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 7a3fb11

Browse files
committed
more helpful wait_for failures
1 parent be0bbc9 commit 7a3fb11

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

tests/test_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ def chapter2_assertions():
14201420
chapter2_assertions()
14211421
self.assertEqual(call_counts['button-output'].value, 0)
14221422
time.sleep(5)
1423-
wait_for(lambda: call_counts['button-output'].value == 1)
1423+
wait_for(lambda: call_counts['button-output'].value, expected_value=1)
14241424
time.sleep(2) # liberally wait for the front-end to process request
14251425
chapter2_assertions()
14261426
assert_clean_console(self)

tests/utils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import time
32

43

@@ -14,16 +13,21 @@ def wrap():
1413
return wrap
1514

1615

17-
1816
class WaitForTimeout(Exception):
1917
"""This should only be raised inside the `wait_for` function."""
2018
pass
2119

2220

23-
def wait_for(condition_function, get_message=lambda: '', *args, **kwargs):
21+
def wait_for(condition_function, expected_value=None, timeout=TIMEOUT,
22+
get_message=None, *args, **kwargs):
2423
"""
25-
Waits for condition_function to return True or raises WaitForTimeout.
26-
:param (function) condition_function: Should return True on success.
24+
Waits for condition_function to return truthy or raises WaitForTimeout.
25+
:param (function) condition_function: Should return truthy or
26+
expected_value on success.
27+
:param (function) get_message: Optional failure message function
28+
:param expected_value: Optional return value to wait for. If omitted,
29+
success is any truthy value.
30+
:param (float) timeout: max seconds to wait. Defaults to 5
2731
:param args: Optional args to pass to condition_function.
2832
:param kwargs: Optional kwargs to pass to condition_function.
2933
if `timeout` is in kwargs, it will be used to override TIMEOUT
@@ -48,19 +52,24 @@ def wrapped_condition_function():
4852
return condition_function(**kwargs)
4953
return condition_function()
5054

51-
if 'timeout' in kwargs:
52-
timeout = kwargs['timeout']
53-
del kwargs['timeout']
54-
else:
55-
timeout = TIMEOUT
56-
5755
start_time = time.time()
5856
while time.time() < start_time + timeout:
59-
if wrapped_condition_function():
57+
condition_val = wrapped_condition_function()
58+
if expected_value is None:
59+
if condition_val:
60+
return True
61+
elif condition_val == expected_value:
6062
return True
6163
time.sleep(0.5)
6264

63-
raise WaitForTimeout(get_message())
65+
if get_message:
66+
message = get_message()
67+
elif expected_value:
68+
message = 'Final value: {}'.format(condition_val)
69+
else:
70+
message = ''
71+
72+
raise WaitForTimeout(message)
6473

6574

6675
def assert_clean_console(TestClass):

0 commit comments

Comments
 (0)