Use for any question or task about testing async Python code, including how to test an async function, async tests that are skipped or fail with event-loop errors, and tests for FastAPI or aiohttp endpoints: pytest-asyncio modes and loop scopes, async fixtures, AsyncMock (assert_awaited), httpx.AsyncClient with ASGITransport and aiohttp_client.
91%
Does it follow best practices?
Impact
97%
1.01xAverage score across 10 eval scenarios
Passed
No findings from the security scan
Await-specific assertions and side_effect semantics for
unittest.mock.AsyncMock (stdlib since Python 3.8). The SKILL.md spine keeps
the primary AsyncMock example; the exhaustive assertion reference is here.
Source: docs.python.org AsyncMock.
@pytest.mark.asyncio
async def test_external_call():
with patch("myapp.clients.redis.get", new_callable=AsyncMock) as mock_get:
mock_get.return_value = b"cached"
result = await fetch_from_cache("key")
mock_get.assert_awaited_once_with("key")
assert result == b"cached"new_callable=AsyncMock is required so patch installs a coroutine-returning
mock rather than a plain MagicMock.
| Assertion | Meaning |
|---|---|
assert_awaited_once_with(*a, **kw) | Awaited exactly once with these args |
assert_awaited_with(*a, **kw) | Last await had these args |
assert_any_await(*a, **kw) | Ever awaited with these args |
assert_not_awaited() | Never awaited |
await_count | How many times awaited (attribute, not assertion) |
The assert_called_* family checks that the mock was called, not that it
was awaited; a coroutine mock can be called without being awaited, so prefer
the assert_awaited_* family for async code.
side_effect on AsyncMock behaves the same as on a sync mock: a callable is
invoked and its result returned; an exception class (or instance) is raised
when the mock is awaited; an iterable returns successive values on each await.