The Problem
You have learned the AI TDD loop manually: write a spec, generate tests, prompt the AI, run tests, iterate. But doing this manually for every function is tedious. In a real team using AI assistants at scale (75+ PRs/day), you need automation.
This project builds a TDD verification pipeline that automates the loop:
Specification (JSON)
↓
Test Generation (from spec)
↓
AI Code Generation (prompt AI with spec + tests)
↓
Test Execution (run pytest)
↓
Failure Analysis (parse test results)
↓
Feedback Loop (feed failures back to AI)
↓
Repeat until all tests pass or max iterations reached
↓
Verified Code + Iteration Log
Pipeline architecture
The pipeline has 5 components:
1. Spec Loader
Reads a JSON specification file containing the behavioral spec (from Lesson 2).
2. Test Generator
Generates pytest test cases from the spec. Uses the SpecToTestTranslator from Lesson 2.
3. AI Code Generator
Prompts the AI assistant with the spec and tests. In this project, we simulate the AI with a simple template-based generator. In production, you would call the Claude API, GitHub Copilot API, or use subprocess to invoke Claude Code / Cursor.
4. Test Runner
Runs pytest against the generated code and parses the results (pass/fail counts, failure messages).
5. Iteration Controller
Manages the loop: feeds failures back to the AI, tracks iterations, detects regressions, and enforces the 5-iteration limit.
The pipeline is a framework, not a one-off script
The pipeline is designed to be reusable: drop in any specification JSON, and it produces verified code. This is the same pattern you will use in Phase 7 (CI/CD pipelines for AI code) -- the pipeline becomes a CI step that automatically verifies AI-generated code before it reaches review.
Demo specifications
The pipeline includes 3 demo specifications:
validate_email -- string validation with edge casescalculate_discount -- numeric calculation with invariantsparse_csv_line -- data parsing with error handling
Each specification includes all 5 components from Lesson 2: functional requirements, input domain, output contract, failure modes, and invariants.
Running the pipeline
python lesson_01_05_tdd_pipeline.py --spec email --output pipeline_report.md
python lesson_01_05_tdd_pipeline.py --spec discount --output pipeline_report.md
python lesson_01_05_tdd_pipeline.py --spec csv --output pipeline_report.md
python lesson_01_05_tdd_pipeline.py --all --output pipeline_report.md
The pipeline produces:
- Generated code file (
generated_code.py) - Test file (
generated_tests.py) - Iteration log with pass/fail counts per iteration
- Final report with verification status
The pipeline runs 5 iterations on the validate_email spec and the AI still cannot pass all tests. What does the pipeline do, and what should the engineer do?
The pipeline enforces the 5-iteration limit from Lesson 3 and produces a diagnostic report. This is by design: the pipeline is not meant to produce code at any cost -- it is meant to produce VERIFIED code or a clear diagnosis of why verification failed. The engineer uses the report to decide: (1) if the spec is ambiguous, they update the spec and re-run, (2) if the function is too complex, they decompose it and run the pipeline on each sub-function, (3) if the AI is stuck in a pattern, they rephrase the prompt. The pipeline automates the routine case (code passes in 1-3 iterations) and flags the exceptional case (code doesn't converge) for human analysis. This is the same escalation pattern from Lesson 3, now automated.