N
NexusDigitalLabs
← Back to Academy

Phase 1 — Foundations · Lesson 11 · 20 XP

Mocking HTTP, ruff, git workflow

A unit test that makes a real network call to a third-party API is slow, flaky (fails when the network or the API does, not just when your code has a bug), and sometimes costs real money. respx (or pytest-httpx) intercepts httpx calls in a test and returns a canned response instead, so the test is fast, deterministic, and needs no API key.

import respx, httpx

@respx.mock
def test_fetch_user():
    respx.get("https://api.example.com/user/1").mock(
        return_value=httpx.Response(200, json={"id": 1, "name": "Ada"})
    )
    # code under test calls the real client; respx intercepts the request

ruff is a single fast tool that replaces flake8 (linting), black (formatting), and isort (import sorting): ruff check finds problems, ruff format fixes style. Pair that with a normal git workflow — a feature branch, small commits, a pull request — and you have the same setup used on real engineering teams.

Exercise

Add a respx mock to one of your Lesson 9 fetcher tests so it runs with zero network access. Run ruff check and ruff format on your project and fix what's flagged. Create a git branch, commit your work, and open a pull request.

Check yourself

1. Why should a unit test never make a real network call to a third-party API?

2. What's the difference between what ruff check does and what ruff format does?

← Previous lesson

Testing with pytest

Answer the check-yourself questions to unlock this