SleepFake
is a compact Python package (under 100 lines) that provides a context manager to simulate the time.sleep
and asyncio.sleep
functions during tests. This is useful for testing time-dependent code without the need to actually wait for time to pass. The real magic behind this package comes from freezegun. 🎩✨
pip install sleepfake
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
@pytest.mark.asyncio
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
import asyncio
import time
from sleepfake import SleepFake
def test_example(sleepfake: SleepFake):
start = time.time()
time.sleep(10)
end = time.time()
assert end - start == 10
@pytest.mark.asyncio
async def test_async_example(sleepfake: 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
Install rye
curl -sSf https://rye.astral.sh/get | bash
rye sync
rye run test
rye run lint