0
# Mock
1
2
Mock is a library for testing in Python that allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. This package contains a rolling backport of the standard library unittest.mock code compatible with Python 3.6 and up.
3
4
## Package Information
5
6
- **Package Name**: mock
7
- **Package Type**: pypi
8
- **Language**: Python
9
- **Installation**: `pip install mock`
10
11
## Core Imports
12
13
```python
14
import mock
15
```
16
17
Common imports for testing:
18
19
```python
20
from mock import Mock, MagicMock, patch, call, ANY, sentinel
21
```
22
23
For async testing:
24
25
```python
26
from mock import AsyncMock
27
```
28
29
## Basic Usage
30
31
```python
32
from mock import Mock, patch, call
33
34
# Create a simple mock object
35
mock_obj = Mock()
36
mock_obj.method.return_value = 'test result'
37
38
# Call the method and assert
39
result = mock_obj.method('arg')
40
assert result == 'test result'
41
mock_obj.method.assert_called_with('arg')
42
43
# Using patch to replace objects temporarily
44
with patch('module.function') as mock_func:
45
mock_func.return_value = 42
46
# Code that calls module.function() will get 42
47
48
# Using decorators for patching
49
@patch('module.function')
50
def test_something(mock_func):
51
mock_func.return_value = 'mocked'
52
# Test code here
53
```
54
55
## Architecture
56
57
The mock library provides a hierarchical set of mock objects and patching utilities:
58
59
- **Mock Objects**: Core mock classes (Mock, MagicMock, AsyncMock) that can replace any object
60
- **Non-callable Mocks**: Specialized mocks for non-callable objects (NonCallableMock, NonCallableMagicMock)
61
- **Patching System**: Context managers and decorators for temporarily replacing objects
62
- **Assertion Helpers**: Built-in methods for validating mock interactions
63
- **Special Objects**: Sentinel values and matching utilities (ANY, call, sentinel)
64
65
This design enables comprehensive test isolation, dependency injection, and behavior verification across synchronous and asynchronous Python code.
66
67
## Capabilities
68
69
### Core Mock Classes
70
71
Mock object creation and configuration, including callable mocks, non-callable mocks, magic method support, and async/await compatibility.
72
73
```python { .api }
74
class Mock: ...
75
class NonCallableMock: ...
76
class MagicMock(Mock): ...
77
class NonCallableMagicMock(NonCallableMock): ...
78
class AsyncMock(Mock): ...
79
class ThreadingMock(Mock): ...
80
class PropertyMock(Mock): ...
81
```
82
83
[Mock Objects](./mock-objects.md)
84
85
### Patching Functions
86
87
Temporary replacement of objects with mocks using context managers and decorators, supporting function patching, object attribute patching, and dictionary patching.
88
89
```python { .api }
90
def patch(target, **kwargs): ...
91
def patch.object(target, attribute, **kwargs): ...
92
def patch.multiple(target, **kwargs): ...
93
class patch.dict: ...
94
```
95
96
[Patching](./patching.md)
97
98
### Utility Functions
99
100
Helper functions for creating specialized mocks and managing mock behavior.
101
102
```python { .api }
103
def create_autospec(spec, **kwargs): ...
104
def mock_open(mock=None, read_data=''): ...
105
def seal(mock): ...
106
```
107
108
[Utilities](./utilities.md)
109
110
### Special Objects and Constants
111
112
Special matching objects and sentinel values for flexible mock assertions and unique object creation.
113
114
```python { .api }
115
class _ANY: ...
116
class _Call: ...
117
class _Sentinel: ...
118
class InvalidSpecError(Exception): ...
119
DEFAULT: object
120
sentinel: _Sentinel
121
ANY: _ANY
122
call: _Call
123
FILTER_DIR: bool
124
```
125
126
[Special Objects](./special-objects.md)