Skip to content

Spy with async function not registering calls until coroutine is awaited #519

@GabrielSalla

Description

@GabrielSalla

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions