pytest-asyncio-httpx-mocking
БесплатноБез исполняемых скриптовНе проверенWhen masking httpx.AsyncClient with unittest.mock in Pytest, AsyncMock must be used instead of MagicMock for async methods like post/get to prevent TypeError…
от Project-N-E-K-O★ 3 303 звезды на GitHubОб этом скилле
httpx.AsyncClient Mocking with AsyncMock
症状
- 在测试中使用
patch.object(httpx.AsyncClient, 'post', return_value=mock_response)。 - 运行时,代码中包含
response = await client.post(...)的地方抛出TypeError: object MagicMock can't be used in 'await' expression。
根本原因
原因 1: 异步函数的返回值必须是 Coroutine
- 问题:
httpx.AsyncClient.post是一个async def方法,调用它会返回一个可等待(awaitable)的协程。 - 为什么发生: 默认的
patch或MagicMock没有自动推断对象的异步特性时,它只是同步地返回了return_value。当事件循环试图await这个同步的MagicMock对象时,就会报错。 - 解决方案: 在
patch参数里显式使用new=AsyncMock(return_value=...)或new_callable=AsyncMock。
代码解决方案
❌ 错误写法:
from unittest.mock import patch, MagicMock
mock_response = MagicMock(status_code=200)
# 当被 await 时会触发 TypeError!
with patch.object(httpx.AsyncClient, 'post', return_value=mock_response):
await my_crawler.fetch()
✅ 正确写法:
from unittest.mock import patch, MagicMock, AsyncMock
mock_response = MagicMock(status_code=200) # Response 对象本身及其方法通常是同步的
# 正确!覆盖掉原来的方法,使其行为成为一个 AsyncMock
with patch.object(httpx.AsyncClient, 'post', new=AsyncMock(return_value=mock_response)):
await my_crawler.fetch()
使用 side_effect 模拟循序多次请求:
with patch.object(httpx.AsyncClient, 'get', new=AsyncMock(side_effect=[mock_1, mock_2])):
...
关键经验
- 针对任何
async def的 Mock,必须保证它被调用时能走协程语境。 - 严格区分 异步的请求方法 与 同步的响应对象:
httpx.AsyncClient.get是异步的(需AsyncMock),但它返回的Response对象上的.json()是同步的(需MagicMock即可)。
Установить pytest-asyncio-httpx-mocking в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл pytest-asyncio-httpx-mocking?
When masking httpx.AsyncClient with unittest.mock in Pytest, AsyncMock must be used instead of MagicMock for async methods like post/get to prevent TypeError when awaited.
Как установить скилл pytest-asyncio-httpx-mocking?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл pytest-asyncio-httpx-mocking запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic