Python Testing Tip #13 / Jan. 7, 2026

Mock HTTP Calls Effortlessly in Python with responses

When your code calls external services, writing reliable tests becomes challenging—you don't want actual network traffic in your test suite. The responses library addresses this by allowing developers to register fake endpoints and expected responses.

Installation

The library can be installed via pip:

pip install responses

Basic Usage

The responses library intercepts requests.get() calls and returns fake JSON data without requiring internet connectivity. This approach enables tests to run quickly and predictably while covering edge cases such as HTTP errors or unexpected payloads.

Advanced Features

Beyond basic mocking, responses supports:

  • Simulating exceptions
  • Dynamic callbacks
  • Tracking call history for deeper assertions

Key Benefit

The library helps developers focus tests on your logic — not the network by eliminating dependency on actual external services during testing.

Share this tip

Get Python Testing Tips in Your Inbox

Practical Python testing advice delivered to your inbox.