Skip to content

async-mock-call-without-await-assertion

Pair positive AsyncMock call assertions with evidence that the mock was awaited.

Why

Calling an AsyncMock records a call before its coroutine is awaited. A positive call assertion can therefore pass when production code silently drops the coroutine. Await assertions cover that gap, while call assertions still detect extra calls whose coroutines were never awaited.

Fix

Keep the call assertion and add an appropriate assert_awaited assertion. For a test that deliberately checks scheduling before execution, use an exact SARJ456 suppression explaining that contract.

Examples

Before — flagged A call assertion does not prove awaiting
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await deliver(send)
send.assert_called_once_with("item")
After — preferred Call and await assertions protect different contracts
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await deliver(send)
send.assert_called_once_with("item")
send.assert_awaited_once_with("item")
Before — flagged A nonnegative await count does not prove execution
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await deliver(send)
assert send.call_count == 1
assert send.await_count >= 0
After — preferred Unresolved await expectations retain their acceptance
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery(expected):
send = AsyncMock()
await deliver(send)
send.assert_called_once()
send.assert_has_awaits(expected)
Before — flagged An empty await history still has nonnegative length
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await deliver(send)
send.assert_called_once()
assert len(send.await_args_list) >= 0
After — preferred A positive await-history length distinguishes a dropped coroutine
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await deliver(send)
send.assert_called_once()
assert len(send.await_args_list) == 1
Before — flagged A spaced positive call state still needs await evidence
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
pending = send()
pending.close()
assert send.called
After — preferred Await evidence covers the same spaced call state
tests/test_delivery.py
from unittest.mock import AsyncMock
async def test_delivery():
send = AsyncMock()
await send()
assert send.called
send.assert_awaited_once()