Skip to content

python/test-sleep

Rule test-sleep
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

A test that waits a fixed time is slow and still fails when the wait is too short. Wait for the result itself, such as a join, an event or a poll with a deadline.

def test_worker():
done = threading.Event()
start(done)
time.sleep(0.5)
assert done.is_set()
assert done.wait(timeout=5)

The rule reports time.sleep and asyncio.sleep in a test file, but not asyncio.sleep(0).

xpd has no automatic fix, so make the change by hand.

A sleep waits a fixed time and hopes the work is done by then, so the test fails on a slow machine and wastes the time on a fast one. Wait for the result itself, with a join, an event or a poll that has a deadline. asyncio.sleep(0) hands control to the event loop and waits for nothing, so the rule leaves it alone.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This test waits a fixed time. Wait for the result itself, such as a join, an event or a poll with a deadline.

Browse the other rules of the Python pack.