Metadata-Version: 2.1
Name: asyncmock
Version: 0.2.0
Summary: Extension to the standard mock framework to support support async
Home-page: https://github.com/timsavage/asyncmock
Author: Tim Savage
Author-email: tim@savage.company
License: BSD-3-Clause
Keywords: testing,mock,asyncio
Platform: any
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.6
Requires-Dist: mock

##########
Async Mock
##########

Awaitable mocks for async code.

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
   :target: https://github.com/ambv/black
      :alt: Once you go Black...

.. image:: https://travis-ci.org/timsavage/asyncmock.svg?branch=master
    :target: https://travis-ci.org/timsavage/asyncmock

.. image:: https://img.shields.io/pypi/l/asyncmock.svg
    :target: https://pypi.python.org/pypi/asyncmock

.. image:: https://img.shields.io/pypi/pyversions/asyncmock.svg
    :target: https://pypi.python.org/pypi/asyncmock

.. image::  https://img.shields.io/pypi/status/asyncmock.svg
    :target: https://pypi.python.org/pypi/asyncmock

.. image:: https://img.shields.io/pypi/implementation/asyncmock.svg
    :target: https://pypi.python.org/pypi/asyncmock



Installation
============

Install using *pip*:

.. code-block:: bash

    pip install asyncmock


Usage
=====

Async Mock is designed as a drop in replacement for a `Mock` object eg:

.. code-block:: python

    my_mock = AsyncMock()

    await my_mock("foo", bar=123)

    my_mock.assert_called_with("foo", bar=123)


This also works with nested methods:

.. code-block:: python

    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:

.. code-block:: python

    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 <https://docs.pytest.org/en/latest/>`_ along with the
`pytest-asyncio <https://github.com/pytest-dev/pytest-asyncio>`_ plugin.

Generating an exception:

.. code-block:: python

    @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()


