python
network_error
ai_generated
true
requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.example.com', port=80): 超过最大重试次数
requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.example.com', port=80): Max retries exceeded
ID: python/pytest-requests-mock-not-active
80%修复率
86%置信度
0证据数
2025-10-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
测试使用 requests_mock 但 mock 未激活或不匹配请求。这导致尝试真实的 HTTP 请求,从而引发连接错误。
English
The test uses requests_mock but the mock is not active or does not match the request. This causes the real HTTP request to be attempted, leading to a connection error.
解决方案
-
98% 成功率
def test_api(requests_mock): requests_mock.get('http://api.example.com/data', json={'key': 'value'}) response = requests.get('http://api.example.com/data') assert response.json() == {'key': 'value'} -
95% 成功率
with requests_mock.Mocker() as m: m.get('http://api.example.com/data', text='ok') requests.get('http://api.example.com/data') -
90% 成功率
Print the request URL in the test to ensure it matches the mock's URL.
无效尝试
常见但无效的做法:
-
90% 失败
This makes tests flaky and dependent on external services.
-
95% 失败
Mocks are synchronous; sleeping does not activate them.
-
80% 失败
This prevents the error but the test still fails because the request is not mocked.