{
  "id": "python/pytest-httpx-mock-not-applied",
  "signature": "httpx.ConnectError: [Errno 111] Connection refused\n# despite using respx / pytest-httpx mock",
  "signature_zh": "httpx.ConnectError: [Errno 111] 连接被拒绝\n# 尽管使用了 respx / pytest-httpx mock",
  "regex": "httpx\\.ConnectError:\\ \\[Errno\\ 111\\]\\ Connection\\ refused\\\n\\#\\ despite\\ using\\ respx\\ /\\ pytest\\-httpx\\ mock",
  "domain": "python",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "The HTTP client was instantiated before the mock was applied, or the mock targets a different client instance than the one used by the code under test.",
  "root_cause_type": "generic",
  "root_cause_zh": "HTTP 客户端在 mock 应用之前被实例化，或 mock 针对的客户端实例与被测代码使用的实例不同。",
  "versions": [
    {
      "version": "0.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "If the client is module-level or created earlier, the transport is already bound and the mock is bypassed.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "No such env var; respx/pytest-httpx don't read it, so the real network is still used.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Fragile against httpx internal changes and misses async variants; can break unrelated tests.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "import respx, httpx\n\ndef test_fetch(respx_mock):\n    respx_mock.get('https://api.example.com/data').mock(\n        return_value=httpx.Response(200, json={'ok': True})\n    )\n    client = httpx.Client()  # created after mock\n    assert client.get('https://api.example.com/data').json()['ok']",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.92,
      "how": "def test_fetch(httpx_mock):\n    httpx_mock.add_response(json={'ok': True})\n    with httpx.Client() as c:\n        assert c.get('https://api.example.com/data').json()['ok']",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2026-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}