Agentic TDD
Test-driven development approach where AI agents write tests first and iteratively implement code to pass them.
Category: AI
Tags: ai, software-engineering, techniques, development-tools
Explanation
Agentic TDD adapts Test-Driven Development (TDD) for AI-assisted workflows. The red-green cycle becomes even more valuable when an agent is writing the implementation, because tests provide the objective verification that the code actually works rather than just looks plausible.
## The red-green cycle with agents
1. **Red**: Write automated tests first and confirm they fail. This step is critical. Skipping it risks creating tests that already pass, which defeats the purpose.
2. **Green**: Let the agent implement code until all tests pass.
3. **Iterate**: The agent can refactor, extend, or fix, always keeping tests green.
Modern coding models understand "use red/green TDD" as shorthand. A prompt like "Build a Python function to extract headers from a markdown string. Use red/green TDD." triggers the full workflow.
## First run the tests
A complementary pattern: always start a session by running existing tests. Four words ("first run the tests") accomplish several things simultaneously:
- **Discovery**: the agent learns a test suite exists and how to run it
- **Complexity assessment**: test output reveals project scale (number of tests acts as a proxy for size)
- **Knowledge**: test code hints at existing features and architecture
- **Mindset**: having run tests, the agent naturally expands them later
## Why this matters for agents specifically
Agent-generated code has three specific risks that TDD addresses:
- **Non-functional code**: agents can generate plausible code that doesn't actually work. Tests catch this.
- **Dead code**: unnecessary implementations that go unused. Tests expose what's actually needed.
- **Regression**: as agents modify code, they risk breaking existing functionality. Tests prevent this.
The old argument against TDD ("tests take too long to write") collapses when agents can generate and maintain test suites quickly. The cost of testing has dropped alongside the cost of coding, removing the main friction that kept teams from adopting TDD.
Related Concepts
← Back to all concepts