[New-bugs-announce] [issue37075] Error message improvement for AsyncMock

Karthikeyan Singaravelan report at bugs.python.org
Tue May 28 05:50:10 EDT 2019


New submission from Karthikeyan Singaravelan <tir.karthi at gmail.com>:

* 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.

----------
components: Library (Lib)
messages: 343753
nosy: asvetlov, lisroach, xtreak
priority: normal
severity: normal
status: open
title: Error message improvement for AsyncMock
type: behavior
versions: Python 3.8

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


More information about the New-bugs-announce mailing list