# requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.example.com', port=80): 超过最大重试次数

- **ID:** `python/pytest-requests-mock-not-active`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试使用 requests_mock 但 mock 未激活或不匹配请求。这导致尝试真实的 HTTP 请求，从而引发连接错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (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'}
   ```
2. **** (95% 成功率)
   ```
   with requests_mock.Mocker() as m:
    m.get('http://api.example.com/data', text='ok')
    requests.get('http://api.example.com/data')
   ```
3. **** (90% 成功率)
   ```
   Print the request URL in the test to ensure it matches the mock's URL.
   ```

## 无效尝试

- **** — This makes tests flaky and dependent on external services. (90% 失败率)
- **** — Mocks are synchronous; sleeping does not activate them. (95% 失败率)
- **** — This prevents the error but the test still fails because the request is not mocked. (80% 失败率)
