python network_error ai_generated true

requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.example.com', port=80): Max retries exceeded

ID: python/pytest-requests-mock-not-active

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-10-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

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.

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

    This makes tests flaky and dependent on external services.

  2. 95% fail

    Mocks are synchronous; sleeping does not activate them.

  3. 80% fail

    This prevents the error but the test still fails because the request is not mocked.