Async Mock
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()
Metadata
Release files for asyncmock 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asyncmock-0.4.2.tar.gz | 3.2 kB | Without provenance | Details for asyncmock-0.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asyncmock-0.4.2-py3-none-any.whl | Python 3 | none | any | Details for asyncmock-0.4.2-py3-none-any.whl |
Total release size: 7.4 kB
Release files / asyncmock-0.4.2.tar.gz
| Download URL | asyncmock-0.4.2.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c251889d542e98fe5f7ece2b5b8643b7d62b50a5657d34a4cbce8a1d5170d750
|
|
BLAKE2b-256 checksum How to use checksums |
c858fa6b3147951a8d82cc78e628dffee0aa5838328c52ebfee4e0ddceb5d92b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.4.0 requests-toolbelt/0.8.0 tqdm/4.32.2 CPython/3.6.9
|
Release files / asyncmock-0.4.2-py3-none-any.whl
| Download URL | asyncmock-0.4.2-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd8bc4e7813251a8959d1140924ccba3adbbc7af885dba7047c67f73c0b664b1
|
|
BLAKE2b-256 checksum How to use checksums |
03e3873f433eca053c92d3cdb9336a379ee025bc1a86d4624ef87bf97a9ac7bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.4.0 requests-toolbelt/0.8.0 tqdm/4.32.2 CPython/3.6.9
|
Security for asyncmock 0.4.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.