{"id":2532,"library":"httmock","title":"HTTMock","description":"HTTMock is a Python library designed for mocking the 'requests' library, enabling developers to simulate third-party API responses and test internal components that rely on HTTP interactions. The current version is 1.4.0, released in October 2020. While functional, its release cadence is slow, suggesting a mature but less actively developed project.","status":"active","version":"1.4.0","language":"en","source_language":"en","source_url":"https://github.com/patrys/httmock","tags":["mocking","requests","testing","http"],"install":[{"cmd":"pip install httmock","lang":"bash","label":"Install HTTMock"}],"dependencies":[{"reason":"HTTMock is a mocking library specifically designed for the 'requests' HTTP library, making 'requests' a functional dependency for its intended use.","package":"requests","optional":false}],"imports":[{"symbol":"HTTMock","correct":"from httmock import HTTMock"},{"symbol":"urlmatch","correct":"from httmock import urlmatch"},{"symbol":"all_requests","correct":"from httmock import all_requests"},{"symbol":"response","correct":"from httmock import response"}],"quickstart":{"code":"from httmock import urlmatch, HTTMock\nimport requests\n\n@urlmatch(netloc=r'(.*\\.)?example\\.com$')\ndef example_mock(url, request):\n    \"\"\"Mocks requests to example.com.\"\"\"\n    print(f\"Mocking request to: {url.geturl()} with method {request.method}\")\n    if request.path == '/data' and request.method == 'GET':\n        return {'status': 'success', 'data': 'mocked content'}\n    return {'status': 'error', 'message': 'Not found'}\n\n# Use HTTMock as a context manager\nwith HTTMock(example_mock):\n    # This request will be intercepted by example_mock\n    r = requests.get('http://example.com/data')\n    print(f\"Response from mocked URL: {r.json()}\")\n\n    # This request will also be intercepted, but return 'Not found'\n    r_post = requests.post('https://www.example.com/other')\n    print(f\"Response from another mocked URL: {r_post.json()}\")\n\n    # This request will NOT be mocked and will go to the actual internet\n    r_google = requests.get('http://google.com/')\n    print(f\"Response from real Google (status: {r_google.status_code})\")","lang":"python","description":"This quickstart demonstrates how to use `httmock` with the `urlmatch` decorator to intercept and respond to HTTP requests made by the `requests` library. The `HTTMock` context manager ensures that the mock is active only for the duration of the `with` block."},"warnings":[{"fix":"Thoroughly test `httmock` with your specific Python and `requests` versions. Consider alternative mocking libraries like `requests-mock` or `respx` if you encounter compatibility problems with newer environments.","message":"The `httmock` library (patrys/httmock) has not seen updates since October 2020. While it specifies Python 2.7 and 3.4+ support, compatibility with newer Python versions (e.g., 3.9+) or very recent releases of the `requests` library may be untested or have subtle, undocumented issues.","severity":"gotcha","affected_versions":"1.4.0"},{"fix":"Always verify the library's GitHub repository or PyPI page to confirm it's the correct Python `httmock` (patrys/httmock).","message":"There are multiple HTTP mocking libraries across different languages (Rust, Go, Node.js) that share similar names like 'httpmock'. Ensure you are referencing documentation and examples specifically for the Python `httmock` library (github.com/patrys/httmock).","severity":"gotcha","affected_versions":"All"},{"fix":"Always wrap your mocked HTTP requests within a `with HTTMock(...)` block to ensure mocks are applied and cleaned up correctly, limiting their scope to the test or function where they are needed.","message":"`HTTMock` is designed to be used as a context manager (e.g., `with HTTMock(...)`). Not using it as such, or failing to properly manage its scope, can lead to mocks persisting longer than intended, interfering with other tests, or causing unexpected behavior in your application.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}