Started happening after I upgraded to Python version 3.13.
Pytest mock version 3.14.1
AsyncMock is only registering calls after the coroutine is awaited, when it should do it when the function is called.
Code to reproduce
File tests/other_module.py
async def a(value):
return value * 2
File tests/test_1.py
from unittest.mock import AsyncMock
import pytest
import other_module
pytestmark = pytest.mark.asyncio(loop_scope="session")
def b(value):
return other_module.a(value)
async def test_1(mocker):
a_spy: AsyncMock = mocker.spy(other_module, "a")
result = b(10)
assert not a_spy.called # This should be True because 'a' was called, but it's False
await result
assert a_spy.called # Now it's True
Expected result: a_spy.called should return True even if the coroutine was not awaited.
I could not reproduce the behavior using unittest's patch.
async def test_1():
with patch("other_module.a") as a_spy:
result = b(10)
> assert not a_spy.called # This should be True because 'a' was called, but it's False
^^^^^^^^^^^^^^^^^^^^^^^
E AssertionError: assert not True
E + where True = <AsyncMock name='a' id='139651671861504'>.called
tests/test_1.py:18: AssertionError
Started happening after I upgraded to Python version 3.13.
Pytest mock version 3.14.1
AsyncMock is only registering calls after the coroutine is awaited, when it should do it when the function is called.
Code to reproduce
File
tests/other_module.pyFile
tests/test_1.pyExpected result:
a_spy.calledshould returnTrueeven if the coroutine was not awaited.I could not reproduce the behavior using unittest's patch.