Skip to content

Commit 2891761

Browse files
committed
Add AsyncMock, tests, docs and additional test for patching a sub-module in a referenced module.
1 parent 604d516 commit 2891761

File tree

6 files changed

+768
-19
lines changed

6 files changed

+768
-19
lines changed

README.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,20 @@ The resulting mock object has the following properties:
114114

115115
The mock object also has the following methods:
116116

117-
* `reset_mock()`: reset the mock object to a clean state. This is useful for when
118-
you want to reuse a mock object.
117+
* `reset_mock()`: reset the mock object to a clean state. This is useful for
118+
when you want to reuse a mock object.
119119
* `assert_called()`: assert that the mock object was called at least once.
120120
* `assert_called_once()`: assert that the mock object was called once.
121121
* `assert_called_with(*args, **kwargs)`: assert that the mock object was last
122-
called in a particular way.
122+
called with the specified arguments.
123123
* `assert_called_once_with(*args, **kwargs)`: assert that the mock object was
124124
called once with the given arguments.
125125
* `assert_any_call(*args, **kwargs)`: assert that the mock object was called at
126126
least once with the given arguments.
127127
* `assert_has_calls(calls, any_order=False)`: assert the mock has been called
128-
with the specified `calls`. If any_order is `False` then the calls must be
129-
sequential. If any_order is `False`` then the calls can be in any order, but
130-
they must all appear in mock_calls.
128+
with the specified `calls`. If `any_order` is `False` then the calls must be
129+
sequential. If `any_order` is `True` then the calls can be in any order, but
130+
they must all appear in `call_args_list`.
131131
* `assert_never_called()`: assert that the mock object was never called.
132132

133133
As a result, given a mock object it is possible to call it, have it behave in
@@ -145,6 +145,57 @@ assert meaning_of_life == 42, "Meaning of life is not H2G2 compliant."
145145
m.assert_called_once()
146146
```
147147

148+
### Asynchronous Mocking
149+
150+
As with the `Mock` class, the `AsyncMock` class provided by uMock allows you to
151+
create and observe mock objects that, rather than being called, can be
152+
`await`-ed in Python (for when you're writing asynchronous code).
153+
154+
This class works in almost exactly the same way as the regular `Mock` class,
155+
but instead of calling it, you `await` it. Furthermore, the properties and
156+
methods on the `AsyncMock` class are named differently to reflect the
157+
`await`-able nature of the object.
158+
159+
An `AsyncMock` object has the following properties:
160+
161+
* `await_count`: the number of times the mock object has been awaited.
162+
* `awaited`: `True` if the mock object was awaited at least once.
163+
* `await_args`: the arguments of the last await on the mock object.
164+
* `await_args_list`: a list of the arguments of each await on the mock object.
165+
166+
An asynchronous mock object also has the following methods:
167+
168+
* `reset_mock()`: reset the mock object to a clean state. This is useful for
169+
when you want to reuse a mock object.
170+
* `assert_awaited()`: assert that the mock object was awaited at least once.
171+
* `assert_awaited_once()`: assert that the mock object was awaited once.
172+
* `assert_awaited_with(*args, **kwargs)`: assert that the mock object was last
173+
awaited with the specified arguments.
174+
* `assert_awaited_once_with(*args, **kwargs)`: assert that the mock object was
175+
awaited once with the given arguments.
176+
* `assert_any_await(*args, **kwargs)`: assert that the mock object was awaited
177+
at least once with the given arguments.
178+
* `assert_has_awaits(awaits, any_order=False)`: assert the mock has been
179+
awaited with the specified awaits. If `any_order` is `False` then the awaits
180+
must be sequential. If `any_order` is `True` then the awaits can be in any
181+
order, but they must all appear in `await_args_list`.
182+
* `assert_not_awaited()`: assert that the mock object was never awaited.
183+
184+
An `AsyncMock` object can be awaited, respond in a specified manner, and you
185+
can interrogate it about how it has been used:
186+
187+
```python
188+
from umock import AsyncMock
189+
190+
191+
m = AsyncMock(return_value=42)
192+
193+
meaning_of_life = await m()
194+
195+
assert meaning_of_life == 42, "Meaning of life is not H2G2 compliant."
196+
m.assert_awaited_once()
197+
```
198+
148199
### Patching
149200

150201
The `patch` class acts as a function decorator or a context manager. Inside the

0 commit comments

Comments
 (0)