Extension to the standard mock framework to support support async
Project description
Awaitable mocks for async code.
Installation
Install using pip:
pip install asyncmock
Usage
Async Mock is designed as a drop in replacement for a Mock object eg:
my_mock = AsyncMock()
await my_mock("foo", bar=123)
my_mock.assert_called_with("foo", bar=123)
This also works with nested methods:
my_mock = AsyncMock()
await my_mock.my_method("foo", bar=123)
my_mock.my_method.assert_called_with("foo", bar=123)
Side effects, return values can also be awaited.
Including a non-awaitable item:
my_mock = AsyncMock()
my_mock.my_method.not_async = True
my_mock.my_method("foo", bar=123)
This can also be provided as an init argument, the not_async argument is not inherited by sub-mocks.
PyTest Example
These examples use pytest along with the pytest-asyncio plugin.
Generating an exception:
@pytest.mark.asyncio
async def test_raise_exception():
my_mock = AsyncMock(side_effect=KeyError)
with pytest.raises(KeyError):
await my_mock()
my_mock.assert_called()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file asyncmock-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: asyncmock-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0403e1e3b2a8f3ef2e9d7bbf5dbb9f12394e5e4f2c2328d6577a65a2bcc8f8b |
|
MD5 | ba4532536a277a2514ac9d27914bfc33 |
|
BLAKE2b-256 | dbca06694f4bc64834373856f44d191ddfff03d3e65c40a2b31083b091dcfb4f |