57.8 Mocking HTTP in Tests with responses and httpretty
Testing HTTP interactions presents a unique challenge. Unlike many parts of an application, you cannot control the external server’s behavior, its response time, or its availability. Relying on live APIs for tests leads to a fragile, slow, and non-deterministic test suite. The solution is to intercept HTTP requests at the library level and return predefined responses, a practice known as mocking. For the modern httpx library, two of the most robust tools for this task are the responses library and httpretty.