This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Error message improvement for AsyncMock
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, lisroach, miss-islington, xtreak
Priority: normal Keywords: patch

Created on 2019-05-28 09:50 by xtreak, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13616 merged xtreak, 2019-05-28 09:52
Messages (2)
msg343753 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-28 09:50
* assert_has_awaits has a comma in between the error message string used in AssertionError and hence error is raised as a tuple instead of a string.

import asyncio
from unittest.mock import AsyncMock, call

async def main():
    a = AsyncMock()
    await a(1)
    a.assert_has_awaits([call(2)])

asyncio.run(main())

Traceback (most recent call last):
  File "/tmp/foo.py", line 9, in <module>
    asyncio.run(main())
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "/tmp/foo.py", line 7, in main
    a.assert_has_awaits([call(2)])
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2164, in assert_has_awaits
    raise AssertionError(
AssertionError: ('Awaits not found.\nExpected: [call(2)]\n', 'Actual: [call(1)]')

It should be printed as a single string as below :

AssertionError: Awaits not found.
Expected: [call(2)]
Actual: [call(1)]

* assert_awaited_with uses _format_mock_failure_message which has "call" hardcoded but most of the error messages in AsyncMock use "await". assert_awaited_with error seems little confusing as it's same as assert_called_with. This is mostly a consistency nit as in below where call to a(2) is made but it's not awaited that should be noted in the message. The fix is simple which would be to make the string 'call' as a parameter that defaults to 'call' and needs to be passed as 'await' from assert_awaited_with error formatting. This would make "expected call not found" as "expected await not found" in below program.

import asyncio
from unittest.mock import AsyncMock, call

async def main():
    a = AsyncMock()
    await a(1)
    a(2)
    a.assert_awaited_with(2)

asyncio.run(main())

./python.exe /tmp/foo.py
/tmp/foo.py:7: RuntimeWarning: coroutine 'AsyncMockMixin._mock_call' was never awaited
  a(2)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/tmp/foo.py", line 10, in <module>
    asyncio.run(main())
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "/tmp/foo.py", line 8, in main
    a.assert_awaited_with(2)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2118, in assert_awaited_with
    raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock(2)
Actual: mock(1)

I will raise a PR for the above shortly.
msg343867 - (view) Author: miss-islington (miss-islington) Date: 2019-05-29 07:02
New changeset 0ae022c6a47abffce22ec185552e319b7b93dbf4 by Miss Islington (bot) (Xtreak) in branch 'master':
bpo-37075: Fix string concatenation in assert_has_awaits error message (GH-13616)
https://github.com/python/cpython/commit/0ae022c6a47abffce22ec185552e319b7b93dbf4
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81256
2019-06-06 07:56:55xtreaksetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-29 07:02:45miss-islingtonsetnosy: + miss-islington
messages: + msg343867
2019-05-28 09:52:53xtreaksetkeywords: + patch
stage: patch review
pull_requests: + pull_request13518
2019-05-28 09:50:10xtreakcreate