Fake the time.sleep/asyncio.sleep function during tests.
Project description
⏰ SleepFake
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. 🎩✨
Installation
pip install sleepfake
🚀 Usage
Context Manager
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
With Fixture (Beta)
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
Local Development
Prerequisites
Install rye
curl -sSf https://rye.astral.sh/get | bash
Install dep
rye sync
Run tests
rye run test
Run linter
rye run lint
Acknowledgments 🙏
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-1.0.7.tar.gz
(6.8 kB
view details)
Built Distribution
File details
Details for the file sleepfake-1.0.7.tar.gz
.
File metadata
- Download URL: sleepfake-1.0.7.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeac46177a62faa6b26019a0e12c14e7c9faf7dd1526df1348e839bfe47b0995 |
|
MD5 | 91eb358302b2ff086f87b47a5b5e60ad |
|
BLAKE2b-256 | 3b993ed321c2822d0f70cc3add8b5a472e196e6d684274454c28b5b53c847354 |
File details
Details for the file sleepfake-1.0.7-py3-none-any.whl
.
File metadata
- Download URL: sleepfake-1.0.7-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 672423a87b04e09ba44e182ace099b9f08474597ec3e6140d9e014c17a1aeb52 |
|
MD5 | 24790d95a045253f6a4564416c6481fb |
|
BLAKE2b-256 | 9a8847dd3d744310d12368f196026516ff8baf22561f37641da1ef7583302cb9 |