Skip to main content

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

AspectTDD (This Chapter)Evals (Chapter 47)
QuestionDoes the code work correctly?Does the LLM reason well?
NatureDeterministicProbabilistic
OutputPass/FailScores (0-1)
SpeedFast (mocked LLM)Slow (real LLM calls)
CostZero (no API calls)High (API calls required)

Skill-First Learning

This chapter follows the Skill-First pattern:

  1. L00: Build your agent-tdd skill from official documentation
  2. L01-L07: Learn patterns that improve your skill
  3. 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

LessonTitleLayerDuration
L00Build Your Testing SkillL1 (Manual)15 min
L01TDD Philosophy for AgentsL1 (Manual)20 min
L02pytest Fundamentals for Async CodeL1 (Manual)25 min
L03Testing FastAPI EndpointsL2 (Collaboration)30 min
L04Testing SQLModel OperationsL2 (Collaboration)25 min
L05Mocking LLM CallsL2 (Collaboration)30 min
L06Testing Agent ToolsL2/L3 (Transition)25 min
L07Integration Test PatternsL3 (Intelligence)30 min
L08Capstone: Full Test SuiteL4 (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:

  1. agent-tdd skill — A reusable testing skill built from official pytest-asyncio and respx documentation
  2. Complete test suite — Unit, integration, and agent pipeline tests for Task API
  3. CI/CD workflow — GitHub Actions configuration for automated testing
  4. 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.