Time
provide.testkit.mocking.time
¶
Time Mocking Utilities.
Provides utilities for mocking time-related functions in tests, particularly sleep functions from both time and asyncio modules.
Classes¶
SleepTracker
¶
Functions¶
create_sleep_mock
¶
Create a mock for sleep functions with tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instant
|
bool
|
If True, sleep calls return immediately. |
True
|
track_calls
|
bool
|
If True, track all sleep calls and durations. |
True
|
Returns:
| Type | Description |
|---|---|
Mock
|
Mock object with sleep tracking capabilities. |
Source code in provide/testkit/mocking/time.py
mock_asyncio_sleep
¶
mock_asyncio_sleep(
instant: bool = True,
track_calls: bool = True,
side_effect: Any = None,
) -> Generator[SleepTracker, None, None]
Context manager to mock only asyncio.sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instant
|
bool
|
If True, sleep calls return immediately. If False, use side_effect. |
True
|
track_calls
|
bool
|
If True, track all sleep calls and durations. |
True
|
side_effect
|
Any
|
Custom side effect for sleep calls. Ignored if instant=True. |
None
|
Yields:
| Type | Description |
|---|---|
SleepTracker
|
SleepTracker instance for inspecting sleep calls. |
Source code in provide/testkit/mocking/time.py
mock_sleep
¶
mock_sleep(
instant: bool = True,
track_calls: bool = True,
side_effect: Any = None,
) -> Generator[SleepTracker, None, None]
Context manager to mock both time.sleep and asyncio.sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instant
|
bool
|
If True, sleep calls return immediately. If False, use side_effect. |
True
|
track_calls
|
bool
|
If True, track all sleep calls and durations. |
True
|
side_effect
|
Any
|
Custom side effect for sleep calls. Ignored if instant=True. |
None
|
Yields:
| Type | Description |
|---|---|
SleepTracker
|
SleepTracker instance for inspecting sleep calls. |
Example
with mock_sleep() as sleep_tracker: time.sleep(1.0) asyncio.sleep(2.0) assert sleep_tracker.call_count == 2 assert sleep_tracker.total_sleep_time == 3.0
Source code in provide/testkit/mocking/time.py
mock_time_sleep
¶
mock_time_sleep(
instant: bool = True,
track_calls: bool = True,
side_effect: Any = None,
) -> Generator[SleepTracker, None, None]
Context manager to mock only time.sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instant
|
bool
|
If True, sleep calls return immediately. If False, use side_effect. |
True
|
track_calls
|
bool
|
If True, track all sleep calls and durations. |
True
|
side_effect
|
Any
|
Custom side effect for sleep calls. Ignored if instant=True. |
None
|
Yields:
| Type | Description |
|---|---|
SleepTracker
|
SleepTracker instance for inspecting sleep calls. |