Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on upstream AsyncMock #130

Open
dimaqq opened this issue Jun 24, 2019 · 3 comments
Open

Question on upstream AsyncMock #130

dimaqq opened this issue Jun 24, 2019 · 3 comments

Comments

@dimaqq
Copy link

dimaqq commented Jun 24, 2019

Thanks to python/cpython#9296, Python3.8 now has unittest.mock.AsyncMock

I wonder if this means that some asynctest use can be pivoted to upstream AsyncMock, in what cases, etc.

Likewise, what features or use cases remain exclusive to asynctest?

Advice is highly appreciated, and could possibly even make it to readme / rtd.

@Martiusweb
Copy link
Owner

Hi,

unittest.mock.AsyncMock borrows most of the concepts and API from asynctest.CoroutineMock.

Enventually (for projects which only support 3.8+), I believe that AsyncMock should become the class to use.
For asynctest, the plan is to make asynctest.CoroutineMock and unittest.AsyncMock the same thing, as they share the same API. The goal is to make the transition to unittest.AsyncMock as easy as possible for asynctest users.

asynctest.TestCase and asynctest.patch still provides features not supported by unittest:

  • TestCase manages the loop, and supports the @fail_on decorator
  • asynctest.patch() supports patching coroutines (ie: decorating a coroutine function or a generator), while afaik, unittest.patch() will not work:
def my_symbol():
   print("I'm not patched")
   
@unittest.patch("module.my_symbol")
async def coroutine():
    # will print "I'm not patched", because the patch is only effective during the construction of the coroutine instance, not when the generator runs
    my_symbol() 

The PR has been merged to CPython one week before I started a new job in a new country, so I don't think I'll have much time to work on asynctest before a while :/

@graingert
Copy link

also note that pypi mock has now backported AsyncMock for older pythons

@graingert
Copy link

graingert commented Feb 21, 2020

perhaps a new release with:

if sys.version_info >= (3, 8):
    from unittest.mock import AsyncMock as CoroutineMock
else:
    from mock import AsyncMock as CoroutineMock

and then depend on:

install_requires =
  pkgutil_resolve_name; python_version < "3.9"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants