Index
provide.testkit.mocking
¶
Mocking utilities for the provide-io ecosystem.
Standardized mocking patterns, fixtures, and utilities to reduce boilerplate and ensure consistent mocking across all tests.
This module re-exports unittest.mock utilities and adds provide-specific testing helpers. Import everything from here for consistency:
from provide.testkit.mocking import Mock, patch, mock_logger
Re-exported from stdlib (unittest.mock): Mock, MagicMock, AsyncMock, PropertyMock - Mock object types patch, call, ANY, DEFAULT - Patching and assertion utilities mock_open - Mock for file operations create_autospec - Create mocks with automatic spec seal - Seal mocks to prevent new attributes sentinel - Create unique sentinel objects
Provide-specific utilities
mock_factory, magic_mock_factory, async_mock_factory, property_mock_factory - Factory fixtures for creating pre-configured mocks patch_fixture, patch_multiple_fixture - Patching with automatic cleanup auto_patch - Unified interface for object/dict/env patching spy_fixture - Create spies that call through to originals assert_mock_calls - Enhanced assertion helper with better errors mock_open_fixture - Fixture wrapper for mock_open
Time mocking utilities (from .time): mock_sleep, mock_time_sleep, mock_asyncio_sleep - Sleep function mocking SleepTracker - Track sleep call history and durations create_sleep_mock - Factory for sleep mocks
Functions¶
assert_mock_calls
¶
Helper for asserting mock calls with better error messages.
Returns:
| Type | Description |
|---|---|
Callable[[Mock, list[Any], bool], None]
|
Function for asserting mock calls. |
Source code in provide/testkit/mocking/fixtures.py
async_mock_factory
¶
Factory for creating AsyncMock objects.
Returns:
| Type | Description |
|---|---|
Callable[..., AsyncMock]
|
Function that creates AsyncMock objects with common configurations. |
Source code in provide/testkit/mocking/fixtures.py
auto_patch
¶
Context manager for automatic patching with cleanup.
Returns:
| Type | Description |
|---|---|
None
|
Patch context manager instance. |
Source code in provide/testkit/mocking/fixtures.py
magic_mock_factory
¶
Factory for creating MagicMock objects.
Returns:
| Type | Description |
|---|---|
Callable[..., MagicMock]
|
Function that creates MagicMock objects with common configurations. |
Source code in provide/testkit/mocking/fixtures.py
mock_factory
¶
Factory for creating configured mock objects.
Returns:
| Type | Description |
|---|---|
Callable[..., Mock]
|
Function that creates mock objects with common configurations. |
Source code in provide/testkit/mocking/fixtures.py
mock_open_fixture
¶
Fixture for mocking file operations.
Returns:
| Type | Description |
|---|---|
Callable[[str | None], Mock]
|
Function that creates a mock for open(). |
Source code in provide/testkit/mocking/fixtures.py
patch_fixture
¶
Fixture for patching objects with automatic cleanup.
Returns:
| Type | Description |
|---|---|
None
|
Function that patches objects and returns the mock. |
Source code in provide/testkit/mocking/fixtures.py
patch_multiple_fixture
¶
Fixture for patching multiple objects at once.
Returns:
| Type | Description |
|---|---|
None
|
Function that patches multiple targets. |
Source code in provide/testkit/mocking/fixtures.py
property_mock_factory
¶
Factory for creating PropertyMock objects.
Returns:
| Type | Description |
|---|---|
Callable[..., PropertyMock]
|
Function that creates PropertyMock objects. |
Source code in provide/testkit/mocking/fixtures.py
spy_fixture
¶
Create a spy (mock that calls through to the original).
Returns:
| Type | Description |
|---|---|
Callable[[Any, str], Mock]
|
Function that creates spy objects. |