APIs with FastAPI · Chapter 21 of 26

Testing APIs

By the end of this chapter you can write pytest tests that call an API with TestClient, share setup through fixtures in conftest.py, give every test a fresh app, replace a dependency in a test, build test data with a factory, give tests their own database, and judge a test by the bugs it would catch. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 4 tasks
  • FastAPI
  • In the all labs plan

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.

Why you need this

Last Tuesday a small change went out: Imran tidied the quote code. On Wednesday Sunita noticed that every 1 kg parcel was being charged for three slabs instead of two. Nobody had tested a weight that is an exact multiple of 500 g, so nothing failed, and three days of merchants were overcharged. Devika's question was short: "how do we know the next change is safe?"

So far your Checks have tested your code. This chapter turns it round: you write the tests, for an API someone else wrote, and the test of your tests is whether they catch real bugs.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    conftest.py: fixtures every test can use

    dakiya.py (do not change it) is DakiyaGo's booking API, with create_app() making a new app with its own empty store. In conftest.py write four fixtures: app, a new app from create_app() for each test; client, a TestClient for that same app; as_merchant, a function where as_merchant("M03") makes the app treat every request as that merchant with no key, by overriding the current_merchant dependency; and book, a factory where book() books a parcel through the client with M01's real X-Api-Key, to 302012, 500 g, cod_amount 0, fails the test unless it gets 201, and returns the parcel's JSON, and book(merchant="M04", weight_grams=1200) changes any of those.

    The check runs tests of its own that use your fixtures: two tests that each book first must both get DKG50001, and an override must not outlive its test.

  2. Task 2

    test_quote.py: test the quote

    Write pytest tests for POST /quote that pass on dakiya.py as it is. The check runs them again on copies of dakiya.py with three bugs planted one at a time, in the slab maths, the cash on delivery fee, and which pincodes get a quote; each copy must make at least one of your tests fail.

    The quote: the zone's first_slab for the first 500 g, each_extra_slab for every further 500 g or part of it, and for cash on delivery 2 percent of the amount with a minimum of Rs 30. An unknown pincode, one not served now, or COD where it is not allowed answers 422.

  3. Task 3

    test_access.py: test who may do what

    Write pytest tests for who may do what in dakiya.py: no key, or a key no merchant has, answers 401; POST /parcels with a good key answers 201 and the parcel with that merchant's id; GET /parcels lists only your own parcels; GET /parcels/{awb} answers your own parcel and 404 for anyone else's.

    The tests must pass on dakiya.py and fail on each of four copies with a bug planted in keys, ownership or status codes. The keys are in /workspace/data/merchants.json.

  4. Task 4 · interview question

    Interview: test_listing.py, passes alone, fails in the suite

    Imran's test in test_listing.py passes when it runs alone and fails when another test file books parcels first. Fix it so it passes alone and in any suite, without making it less strict: it must still fail if the listing loses a parcel or shows them in the wrong order.

    The check runs it alone, after another test file that books parcels on dakiya.app, and on copies of dakiya.py with the listing broken. The interviewer will ask: what makes a test pass alone and fail in the suite, and how do you design tests so it cannot happen?

What you will learn

  • API tests with TestClient and pytest.
  • Fixtures in conftest.py, and why each test needs a fresh app.
  • Dependency overrides and factories for test data.
  • A database per test run, rolled back after every test.
  • What makes a test good: boundaries, exact codes, expected values worked out by hand.

More in Production

Build "Testing APIs" yourself

DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.