[New-bugs-announce] [issue38093] Update MagicMock __aenter__ and __aexit__ to return AsyncMock's

Lisa Roach report at bugs.python.org
Tue Sep 10 10:42:41 EDT 2019


New submission from Lisa Roach <lisaroach14 at gmail.com>:

After a discussion with Michael, xtreak, and Ezio we've decided to try to make the process of mocking an async context manager slightly easier.

Currently if you want to mock a context manager that is used like this:

async with cm():  # note that cm is called here
  blah

You need to mock cm as a MagicMock and set __aenter__ and __aexit__ return values directly because they do not exist on MagicMocks.


Our first step to making this easier is setting the calculated return value of MagicMock __aenter__ and __aexit__ to be AsyncMock, which will make it so you do not need to set them specifically.


A future improvement may be to create a ContextManagerMock directly that can take an async kwarg that would return a context manager with AsyncMocks for __aenter__ and __aexit__ automatically.

----------
assignee: lisroach
messages: 351682
nosy: ezio.melotti, lisroach, michael.foord, xtreak
priority: normal
severity: normal
stage: needs patch
status: open
title: Update MagicMock __aenter__ and __aexit__ to return AsyncMock's
type: behavior
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38093>
_______________________________________


More information about the New-bugs-announce mailing list