Python Testing Tip #13 / Jan. 7, 2026

Mock HTTP Calls Effortlessly in Python with responses

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.

📦 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.

Share this tip

The complete testing system, not just tips.

Stop piecing together advice from blog posts. This course gives you a structured approach to Python testing that scales with your codebase and keeps your AI agents in check.

Get the Course $20