Fake the time.sleep/asyncio.sleep function during tests.
Project description
SleepFake
SleepFake
is a Python class that provides a context manager to fake the time.sleep
and asyncio.sleep
functions during tests. This is useful for testing time-dependent code without having to actually wait for time to pass.
Simple package, the most import magic comes from freezegun.
Usage
import asyncio
import time
from sleepfake import SleepFake
def test_example():
real_start = time.time()
with SleepFake():
start = time.time()
time.sleep(10)
end = time.time()
assert end - start == 10
real_end = time.time()
assert real_end - real_start < 1
async def test_async_example():
real_start = asyncio.get_event_loop().time()
with SleepFake():
start = asyncio.get_event_loop().time()
await asyncio.gather(asyncio.sleep(5), asyncio.sleep(5), asyncio.sleep(5))
end = asyncio.get_event_loop().time()
assert end - start <= 5.5 # almost 5 seconds # noqa: PLR2004
assert end - start >= 5 # almost 5 seconds # noqa: PLR2004
real_end = asyncio.get_event_loop().time()
assert real_end - real_start < 1 # almost 0 seconds
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sleepfake-0.1.0.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for sleepfake-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68484bb00075a19fd94aa7d0ab14cf1793b9754c672ef05f2dfe474fbf8fb9bc |
|
MD5 | 0c76c766669e34efed5a7ce27c0a94fe |
|
BLAKE2b-256 | a666f9c79fcd7daaa1e4f23be00eef5d355fbb342061b5c583382672d28adea9 |