python
network_error
ai_generated
true
httpx.ConnectError: 所有连接尝试均失败 在处理上述异常时,又发生了一个异常: httpx.HTTPStatusError: 客户端错误 '404 Not Found',URL 为 'https://api.example.com/v1/users'
httpx.ConnectError: All connection attempts failed During handling of the above exception, another exception occurred: httpx.HTTPStatusError: Client error '404 Not Found' for url 'https://api.example.com/v1/users'
ID: python/pytest-httpx-mock-not-matched
80%修复率
84%置信度
0证据数
2025-01-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.21.x | active | — | — | — |
| 0.22.x | active | — | — | — |
根因分析
respx/httpx mock 注册的 URL 模式与实际请求不匹配(尾部斜杠、查询字符串或 http 与 https),导致请求穿透到真实网络。
English
respx/httpx mock was registered with a URL pattern that does not match the actual request (trailing slash, query string, or http vs https), so the request falls through to the real network.
解决方案
-
93% 成功率
import respx, httpx @respx.mock async def test_users(): respx.get(url__regex=r'/v1/users.*').mock( return_value=httpx.Response(200, json={'id': 1}) ) async with httpx.AsyncClient(base_url='https://api.example.com') as c: r = await c.get('/v1/users') assert r.status_code == 200 -
88% 成功率
respx.get('https://api.example.com/v1/users').mock(...) # Enable debug: import logging logging.basicConfig(level=logging.DEBUG) # Look for 'No mock matched' in output
无效尝试
常见但无效的做法:
-
85% 失败
Disabling assertions hides the mismatch but the request still goes to the real network and fails with ConnectError.
-
90% 失败
The failure is not a timeout; the URL never matches a registered route so no mock intercepts it.