Chapter 46: TDD for Agents
Part 6: AI Native Software Development — Phase 4: Quality Assurance
Test your agent code with confidence. This chapter teaches Test-Driven Development (TDD) for AI agent applications—testing code correctness (deterministic, passes/fails), not LLM reasoning quality (that's Chapter 47: Evals).
What You'll Build
By the end of this chapter, you'll own a production-ready agent-tdd skill and a comprehensive test suite for your Task API with:
- 80%+ code coverage on your agent API
- Zero LLM API calls during test execution
- Sub-10-second test runs with mocked dependencies
- CI/CD automation via GitHub Actions
The Critical Distinction
| Aspect | TDD (This Chapter) | Evals (Chapter 47) |
|---|---|---|
| Question | Does the code work correctly? | Does the LLM reason well? |
| Nature | Deterministic | Probabilistic |
| Output | Pass/Fail | Scores (0-1) |
| Speed | Fast (mocked LLM) | Slow (real LLM calls) |
| Cost | Zero (no API calls) | High (API calls required) |
Skill-First Learning
This chapter follows the Skill-First pattern:
- L00: Build your
agent-tddskill from official documentation - L01-L07: Learn patterns that improve your skill
- L08: Compose everything into a production test suite
Every lesson ends with "Reflect on Your Skill"—you'll continuously test and improve the skill you own.
Chapter Structure
| Lesson | Title | Layer | Duration |
|---|---|---|---|
| L00 | Build Your Testing Skill | L1 (Manual) | 15 min |
| L01 | TDD Philosophy for Agents | L1 (Manual) | 20 min |
| L02 | pytest Fundamentals for Async Code | L1 (Manual) | 25 min |
| L03 | Testing FastAPI Endpoints | L2 (Collaboration) | 30 min |
| L04 | Testing SQLModel Operations | L2 (Collaboration) | 25 min |
| L05 | Mocking LLM Calls | L2 (Collaboration) | 30 min |
| L06 | Testing Agent Tools | L2/L3 (Transition) | 25 min |
| L07 | Integration Test Patterns | L3 (Intelligence) | 30 min |
| L08 | Capstone: Full Test Suite | L4 (Spec-Driven) | 35 min |
Total Duration: ~4 hours
Prerequisites
Before starting this chapter, you should have:
- Chapter 40: FastAPI for Agents (Task API codebase)
- Part 5: Python fundamentals (basic pytest knowledge)
- Ch34-36: Agent SDK patterns (code to test)
What You'll Own
By completing this chapter, you'll have:
agent-tddskill — A reusable testing skill built from official pytest-asyncio and respx documentation- Complete test suite — Unit, integration, and agent pipeline tests for Task API
- CI/CD workflow — GitHub Actions configuration for automated testing
- Testing patterns — Transferable patterns for any FastAPI + SQLModel project
Key Technologies
- pytest-asyncio — Async test execution
- httpx — Async HTTP client with ASGITransport
- respx — HTTP mocking for LLM API calls
- pytest-cov — Coverage reporting
Start Learning
Begin with Lesson 0: Build Your Testing Skill to create your agent-tdd skill before learning the content.