Extension to the standard mock framework to support support async
Project description
Awaitable mocks for async code.
The package specifically only extends mock and not any other part of unittest.
Installation
Install using pip:
pip install asyncmock
Usage
Async Mock is 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 and 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)
The not_async option 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 Distribution
Built Distribution
File details
Details for the file asyncmock-0.4.1.tar.gz
.
File metadata
- Download URL: asyncmock-0.4.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.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 | 34fb83b1b9ebe5b2a410af09889a5fe316ba9cc7a2a2605c63157ea0a1425d28 |
|
MD5 | 37f63f7c45ea0f468f04e9842c6a34d6 |
|
BLAKE2b-256 | b45a7c57d4275044f433bd11daca1e9bc80711de999013e566493e18d8923a18 |
File details
Details for the file asyncmock-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: asyncmock-0.4.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.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 | db12e484fb37b7d6cecc496f15515ecb1bf6ff03ce1be0ec4789b14454a82409 |
|
MD5 | bed2e80aa97467839f5017dc97799c29 |
|
BLAKE2b-256 | 1b2477a8ffd556714553aba24ea99c84f5b088d82b0bbd7e95d6d1cf99ee8938 |