# requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.example.com', port=443): 超过最大重试次数，URL: /v1/items（由 NewConnectionError(...) 引起）

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

## 根因

requests_mock.Mocker 作为上下文管理器使用，但被测客户端创建了自己的 Session（或直接使用 requests.Session()），并非挂载了适配器的那个 session。

## 版本兼容性

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

## 解决方案

1. **** (94% 成功率)
   ```
   import requests, requests_mock

def test_fetch():
    session = requests.Session()
    with requests_mock.Mocker(session=session) as m:
        m.get('https://api.example.com/v1/items', json=[])
        client = MyClient(session=session)
        assert client.fetch_items() == []
   ```
2. **** (92% 成功率)
   ```
   @pytest.fixture
def mocked_session():
    s = requests.Session()
    with requests_mock.Mocker(session=s) as m:
        yield s, m

def test_x(mocked_session):
    s, m = mocked_session
    m.get('https://api.example.com/v1/items', json=[])
    assert MyClient(session=s).fetch_items() == []
   ```

## 无效尝试

- **** — Module-level mocker still only patches the default session; code that instantiates its own Session bypasses it. (85% 失败率)
- **** — Falls back to the real network, which is exactly what CI cannot access; the ConnectionError persists. (90% 失败率)
