Module 3: Integration and E2E Tests

Module 3: Integration and E2E Tests

Module 3: Integration and E2E Tests

Capsule overview

A unit test tells you that calculate_discount(100, 10) returns 90. But it doesn't tell you whether the POST /orders endpoint correctly applies that discount when it receives a real HTTP request. And it certainly doesn't tell you whether the complete flow — create user → add items → apply discount → confirm order — works from start to finish.

In the previous modules you mastered unit tests: isolated functions, inputs and outputs, fast and focused tests. Now you're going to climb the test pyramid: integration tests that verify how the components interact (endpoints, database, services), and E2E tests that validate complete user flows.

This module gives you strategic judgment: how many tests at each level, when the cost of an E2E test is justified, and how to use Claude Code to generate tests at all three levels with prompts specific to each one. By the end, you'll have a complete test pyramid — the professional foundation for any serious project.


Module Context

Where are we?

This is Module 3 — the last of Phase 1 (TDD Fundamentals with AI).

What you already know:

  • ✅ Spec-first methodology and TDD with AI (Module 1)
  • ✅ Generating and evaluating unit tests with Claude Code (Module 2)
  • ✅ Pytest patterns: AAA, parametrize, fixtures, naming (Module 2)

What's missing: Tests beyond isolated functions. Verifying that endpoints, databases, and complete flows work correctly.

Where are we headed?

Phase 1: TDD Fundamentals with AI
├── Module 1: ✅ Spec-first (philosophy)
├── Module 2: ✅ Unit tests (base technique)
└── Module 3: ← YOU ARE HERE — Integration and E2E (complete pyramid)

Phase 2: Testing Workflows
├── Module 4: → Complete TDD workflow (red-green-refactor with AI)
├── Module 5: → Coverage and edge cases
└── Module 6: → Mocking, fixtures, validation loops

After this module you complete Phase 1 and have all the fundamental pieces. Phase 2 assembles them into productive workflows.


Professional Objective

Build complete test pyramids with tests at all three levels — unit, integration, E2E — and have the judgment to decide how many tests at each level according to the project.

By the end of this module you'll be able to:

  • ✅ Explain the test pyramid and the trade-offs of each level (speed vs confidence vs cost)
  • ✅ Use FastAPI TestClient for integration testing of HTTP endpoints
  • ✅ Create fixtures for database testing with automatic setup and teardown
  • ✅ Implement E2E tests that validate complete flows via the API
  • ✅ Generate tests at each level with Claude Code using differentiated prompts
  • ✅ Organize tests by level: tests/unit/, tests/integration/, tests/e2e/

Module Progression

Module Map

CapsuleTopicWhat you'll learn
02Test Pyramid and trade-offsThe 3 levels, when to use each, how many tests per level, the pyramid as a strategy
03API testing with FastAPI TestClientIntegration testing of endpoints: GET, POST, PUT, DELETE with assertions on status, body, headers
04Database testing with fixturesTest databases, seed data, teardown, fixtures for isolation between tests
05E2E testing — complete flowsTests that validate complete user flows via HTTP: create → read → update → delete
06Project: Complete test pyramidBuild unit + integration + E2E tests for an item REST API with Claude Code

Learning flow

First you'll understand the strategy (capsule 02): the test pyramid as a decision framework. It's not "write tests at all 3 levels" — it's "understand the trade-offs and decide how many tests at each level according to your project."

Then you'll master integration testing (capsules 03-04): testing HTTP endpoints with FastAPI TestClient and testing interactions with databases using fixtures. This is the middle level of the pyramid — more confidence than unit tests, faster than E2E.

Next you'll experience E2E testing (capsule 05): complete flows that simulate what a real user of your API would do. Create resource → get → update → delete → verify it doesn't exist. It's the highest level of confidence but also the most costly.

In the project (capsule 06) you'll build a complete pyramid: unit tests for business logic, integration tests for endpoints, and E2E tests for flows. Claude Code generates tests at each level with differentiated prompts.


Project Connection

This module's project: Complete test pyramid

You'll build tests at all three levels for a simple item-management REST API (CRUD). The API is already implemented — your job is to create the test pyramid with Claude Code.

tests/
├── unit/              ← Tests of isolated functions (business logic)
├── integration/       ← Tests of HTTP endpoints (TestClient)
└── e2e/               ← Tests of complete flows (create→read→update→delete)

Connection with the final project (Module 8)

The final project requires tests at all three levels. The pyramid you build here is the exact template you'll replicate at a larger scale. If you master the trade-offs here, you'll know exactly what to test at each level of the final project.


Boundaries: What This Module Will NOT Do

  • ❌ UI testing (Selenium/Playwright) — E2E in this guide means API flows, not browser testing
  • ❌ Microservices with Docker Compose — Integration testing here is TestClient, not service orchestration
  • ❌ Mocking of external services — Covered in Module 6. Here we test real components
  • ❌ Coverage metrics — Covered in Module 5. Here we evaluate the pyramid by judgment, not by numbers
  • ❌ Performance testing — Outside the scope of this guide

Evidence of Success

By the end of this module, you'll know you succeeded if:

  • ✅ You can explain the test pyramid and decide how many tests at each level for a given project
  • ✅ You can test a FastAPI endpoint with TestClient (status code, response body, headers)
  • ✅ You can create fixtures that set up and clean a test database
  • ✅ You can write an E2E test that validates a complete CRUD flow
  • ✅ You completed the project: a complete pyramid with tests at all 3 levels

Summary

  • ✅ This module completes Phase 1 by expanding from unit tests to integration and E2E
  • ✅ The test pyramid is a decision strategy: many unit, some integration, few E2E
  • ✅ Integration tests verify endpoints and interactions between components
  • ✅ E2E tests validate complete user flows via the API
  • ✅ Claude Code generates tests at each level with differentiated prompts
  • ✅ The project produces a complete test pyramid for a REST API

Next capsule: Test Pyramid and trade-offs — the strategy behind the three levels.


Additional Resources

  1. Martin Fowler: Test Pyramid - The original article on the test pyramid
  2. FastAPI Testing Tutorial - Official documentation on testing with FastAPI
  3. pytest Fixtures - Fixtures for database testing
  4. httpx Documentation - HTTP client for E2E testing
  5. Ham Vocke: The Practical Test Pyramid - A detailed practical guide to the pyramid

Module 3 — Testing with Claude Code Guide From isolated functions to complete systems