Is this bug or not? (unittest.mock)

When AsyncMock raises side_effect, then this doesn’t count as await. But when MagicMock raises side_effect, then this still count as call. Tests:

import asyncio
from unittest.mock import AsyncMock

my_mock = AsyncMock(side_effect=ValueError('test'))
try:
    asyncio.run(my_mock())
except ValueError:
    pass
print(my_mock.await_count)

print out 0, but:

from unittest.mock import MagicMock

my_mock = MagicMock(side_effect=ValueError('test'))
try:
    my_mock()
except ValueError:
    pass
print(my_mock.call_count)

print out 1.

1 Like

AsyncMock.call_count used to fail to update calls list on exception. This was fixed in commit b2744c1 (Nov 22, 2019), which should have been in Python 3.8.1. My guess is you’re using Python 3.8.0 (which is the 3.8 in Ubuntu 19.10)