MagicMock doesn't implement `__aenter__` or `__aexit__`
See original GitHub issueWhen testing asyncio code, the probability of needing to test async context manager is high.
The current code using MagicMock doesn’t implement __aenter__ or __aexit__ methods
Issue Analytics
- State:
- Created 7 years ago
- Reactions:27
- Comments:42 (16 by maintainers)
Top Results From Across the Web
python - Mock for context-manager fails with AttributeError: __ ...
The issue is when you're calling mo(..) the object it is returning( tuple ) doesn't have __enter__ and __exit__ attributes on it. To...
Read more >unittest.mock — mock object library — Python 3.11.1 ...
mock provides a core Mock class removing the need to create a host of stubs throughout your test suite. After performing an action,...
Read more >Mocking — asynctest 0.12.3 documentation
asynctest will mock an attribute as a CoroutineMock if the function is a native coroutine ( async def function) or a decorated generator...
Read more >Testing asynchronous context managers in Python
The asynctest module enhances standard unittest.mock to deal with ... but the __aenter__ and __aexit__ methods required by asynchronous ...
Read more >unittest's new context methods in Python 3.11 (with backports)
... logic in many cases, such as dynamic use of unittest.mock. ... Call addCleanup to schedule the context manager's __exit__ to run at ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

We had the same problem mocking aiopg. We ended up with this workaround:
nice work @Martiusweb on a real fix and @Galbar on work-around, this thread inspired us to do a simpler fix:
with usage: