Mocking
provide.testkit.time.mocking
¶
Time mocking fixtures for testing.
Fixtures for mocking time-related functions like sleep, datetime.now(), etc.
Functions¶
mock_datetime
¶
Mock datetime module for testing.
Returns:
| Type | Description |
|---|---|
None
|
Mock datetime module with common methods mocked. |
Example
def test_with_mock_datetime(mock_datetime): ... now = datetime.datetime.now() ... assert now == datetime.datetime(2024, 1, 1, 12, 0, 0)
Source code in provide/testkit/time/mocking.py
mock_sleep
¶
Mock time.sleep to speed up tests.
Returns:
| Type | Description |
|---|---|
None
|
Mock object that replaces time.sleep. |
Example
def test_with_mock_sleep(mock_sleep): ... time.sleep(10) # Returns instantly ... assert mock_sleep.called
Source code in provide/testkit/time/mocking.py
mock_sleep_with_callback
¶
Mock time.sleep with a callback for each sleep call.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[float], None] | None], Mock]
|
Function to set up sleep mock with callback. |
Example
def test_with_callback(mock_sleep_with_callback): ... total_sleep = [] ... sleep_mock = mock_sleep_with_callback(lambda s: total_sleep.append(s)) ... with patch("time.sleep", sleep_mock): ... time.sleep(1.5) ... assert total_sleep == [1.5]
Source code in provide/testkit/time/mocking.py
time_travel
¶
Fixture for traveling through time in tests.
Returns:
| Type | Description |
|---|---|
None
|
Function to travel to specific time points. |
Example
def test_with_time_travel(time_travel): ... time_travel(datetime.datetime(2025, 1, 1)) ... # time.time() now returns the timestamp for 2025-01-01