Mock HTTP Calls Effortlessly in Python with responses

January 7, 2026

When your code calls external services, writing reliable tests can be tricky — you don’t want real network traffic in your test suite. That’s where responses come in:
➡️ A utility for mocking Python’s requests library in tests. GitHub

With responses, you register fake endpoints and expected responses. Tests run fast, are predictable, and cover edge cases like HTTP errors or unexpected payloads — without ever hitting the network.

Complete Python Testing Guide

Most test suites break every time you refactor, take forever to write, and still miss the bugs that matter. Learn how to write pytest tests that survive change, go fast, and actually catch regressions.

Take Course

📦 Install

pip install responses

🧪 Basic Usage

# test_responses.py
import responses
import requests

@responses.activate
def test_not_found():
    responses.add(
        responses.GET,
        "https://api.example.com/user/42",
        json={"error": "not found"},
        status=404
    )

    resp = requests.get("https://api.example.com/user/42")
    assert resp.status_code == 404
    assert resp.json()["error"] == "not found"

Here, the test intercepts the requests.get() call and returns your fake JSON — no internet required. On Test Automation

Responses can also simulate exceptions, dynamic callbacks, and track call history for deeper assertions. Use it to focus tests on your logic — not the network.

Become a better engineer, one article at a time.

Practices, mindsets, and habits that actually move the needle. Delivered weekly to your inbox.

Share