asyncmock


Nameasyncmock JSON
Version 0.4.2 PyPI version JSON
download
home_pagehttps://github.com/timsavage/asyncmock
SummaryExtension to the standard mock framework to support support async
upload_time2020-03-15 21:09:12
maintainer
docs_urlNone
authorTim Savage
requires_python>=3.6
licenseBSD-3-Clause
keywords testing mock asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ##########
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

The package specifically only extends mock_ and not any other part of unittest.

.. _mock: https://mock.readthedocs.io/en/latest/


.. note::
   Mock 4.0+ (included within Python 3.8+) now includes an awaitable mock ``mock.AsyncMock``. 
   This is recommended for new projects.


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

Install using *pip*:

.. code-block:: bash

    pip install asyncmock


Usage
=====

Async Mock is 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 and 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)


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.

.. _pytest: https://docs.pytest.org/en/latest/
.. _pytest-asyncio: https://github.com/pytest-dev/pytest-asyncio


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




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/timsavage/asyncmock",
    "name": "asyncmock",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "testing,mock,asyncio",
    "author": "Tim Savage",
    "author_email": "tim@savage.company",
    "download_url": "https://files.pythonhosted.org/packages/c8/58/fa6b3147951a8d82cc78e628dffee0aa5838328c52ebfee4e0ddceb5d92b/asyncmock-0.4.2.tar.gz",
    "platform": "any",
    "description": "##########\nAsync Mock\n##########\n\nAwaitable mocks for async code.\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n   :target: https://github.com/ambv/black\n      :alt: Once you go Black...\n\n.. image:: https://travis-ci.org/timsavage/asyncmock.svg?branch=master\n    :target: https://travis-ci.org/timsavage/asyncmock\n\n.. image:: https://img.shields.io/pypi/l/asyncmock.svg\n    :target: https://pypi.python.org/pypi/asyncmock\n\n.. image:: https://img.shields.io/pypi/pyversions/asyncmock.svg\n    :target: https://pypi.python.org/pypi/asyncmock\n\n.. image::  https://img.shields.io/pypi/status/asyncmock.svg\n    :target: https://pypi.python.org/pypi/asyncmock\n\n.. image:: https://img.shields.io/pypi/implementation/asyncmock.svg\n    :target: https://pypi.python.org/pypi/asyncmock\n\nThe package specifically only extends mock_ and not any other part of unittest.\n\n.. _mock: https://mock.readthedocs.io/en/latest/\n\n\n.. note::\n   Mock 4.0+ (included within Python 3.8+) now includes an awaitable mock ``mock.AsyncMock``. \n   This is recommended for new projects.\n\n\nInstallation\n============\n\nInstall using *pip*:\n\n.. code-block:: bash\n\n    pip install asyncmock\n\n\nUsage\n=====\n\nAsync Mock is a drop in replacement for a `Mock` object eg:\n\n.. code-block:: python\n\n    my_mock = AsyncMock()\n\n    await my_mock(\"foo\", bar=123)\n\n    my_mock.assert_called_with(\"foo\", bar=123)\n\n\nThis also works with nested methods:\n\n.. code-block:: python\n\n    my_mock = AsyncMock()\n\n    await my_mock.my_method(\"foo\", bar=123)\n\n    my_mock.my_method.assert_called_with(\"foo\", bar=123)\n\n\nSide effects and return values can also be awaited.\n\nIncluding a non-awaitable item:\n\n.. code-block:: python\n\n    my_mock = AsyncMock()\n\n    my_mock.my_method.not_async = True\n    my_mock.my_method(\"foo\", bar=123)\n\n\nThe `not_async` option can also be provided as an init argument. The `not_async` \nargument is not inherited by sub-mocks.\n\n\npytest Example\n==============\n\nThese examples use pytest_ along with the pytest-asyncio_ plugin.\n\n.. _pytest: https://docs.pytest.org/en/latest/\n.. _pytest-asyncio: https://github.com/pytest-dev/pytest-asyncio\n\n\nGenerating an exception:\n\n.. code-block:: python\n\n    @pytest.mark.asyncio\n    async def test_raise_exception():\n        my_mock = AsyncMock(side_effect=KeyError)\n\n        with pytest.raises(KeyError):\n            await my_mock()\n\n        my_mock.assert_called()\n\n\n\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Extension to the standard mock framework to support support async",
    "version": "0.4.2",
    "split_keywords": [
        "testing",
        "mock",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "5a288d2806d6b421539ca64a592be721",
                "sha256": "fd8bc4e7813251a8959d1140924ccba3adbbc7af885dba7047c67f73c0b664b1"
            },
            "downloads": -1,
            "filename": "asyncmock-0.4.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a288d2806d6b421539ca64a592be721",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 4190,
            "upload_time": "2020-03-15T21:09:09",
            "upload_time_iso_8601": "2020-03-15T21:09:09.066660Z",
            "url": "https://files.pythonhosted.org/packages/03/e3/873f433eca053c92d3cdb9336a379ee025bc1a86d4624ef87bf97a9ac7bc/asyncmock-0.4.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "81d051245651fe6f6cbc7b2ecdbc70b5",
                "sha256": "c251889d542e98fe5f7ece2b5b8643b7d62b50a5657d34a4cbce8a1d5170d750"
            },
            "downloads": -1,
            "filename": "asyncmock-0.4.2.tar.gz",
            "has_sig": false,
            "md5_digest": "81d051245651fe6f6cbc7b2ecdbc70b5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 3191,
            "upload_time": "2020-03-15T21:09:12",
            "upload_time_iso_8601": "2020-03-15T21:09:12.858415Z",
            "url": "https://files.pythonhosted.org/packages/c8/58/fa6b3147951a8d82cc78e628dffee0aa5838328c52ebfee4e0ddceb5d92b/asyncmock-0.4.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-03-15 21:09:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "timsavage",
    "github_project": "asyncmock",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "asyncmock"
}
        
Elapsed time: 0.04920s