python
network_error
ai_generated
true
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%Fix Rate
84%Confidence
0Evidence
2025-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 0.21.x | active | — | — | — |
| 0.22.x | active | — | — | — |
Root Cause
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.
generic中文
respx/httpx mock 注册的 URL 模式与实际请求不匹配(尾部斜杠、查询字符串或 http 与 https),导致请求穿透到真实网络。
Workarounds
-
93% success
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% success
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
Dead Ends
Common approaches that don't work:
-
85% fail
Disabling assertions hides the mismatch but the request still goes to the real network and fails with ConnectError.
-
90% fail
The failure is not a timeout; the URL never matches a registered route so no mock intercepts it.