Test-Driven Development with AI
TDD with an AI agent is a different experience than traditional TDD. Claude can write both the tests and the implementation, but the workflow structure still matters.
The AI-Assisted TDD Loop
Section titled “The AI-Assisted TDD Loop”Traditional TDD
Section titled “Traditional TDD”- Write a failing test
- Write code to pass the test
- Refactor
AI-Assisted TDD
Section titled “AI-Assisted TDD”- Describe the behavior you want
- Claude writes tests based on your description
- You review the tests (are they testing the right things?)
- Claude writes the implementation to pass the tests
- You run and verify
Why TDD Still Matters with AI
Section titled “Why TDD Still Matters with AI”“If Claude writes both the tests and code, what’s the point?”
Good question. The value is:
- Tests as specification — you review tests to verify Claude understood your intent
- Regression safety — the tests catch future breakage
- Confidence in refactoring — you can ask Claude to refactor knowing tests will catch issues
- Better code — code written to be testable is usually better code
Workflow Example
Section titled “Workflow Example”> I need a function that validates email addresses.> Requirements:> - Must contain @ and a domain> - Domain must have at least one dot> - No spaces allowed> - Maximum 254 characters>> Write the tests first.Claude writes tests:
describe('validateEmail', () => { it('accepts valid email addresses', () => { expect(validateEmail('user@example.com')).toBe(true); expect(validateEmail('user.name@domain.co.uk')).toBe(true); });
it('rejects emails without @', () => { expect(validateEmail('userexample.com')).toBe(false); });
it('rejects emails without domain dot', () => { expect(validateEmail('user@localhost')).toBe(false); });
it('rejects emails with spaces', () => { expect(validateEmail('user @example.com')).toBe(false); });
it('rejects emails over 254 characters', () => { const longEmail = 'a'.repeat(245) + '@example.com'; expect(validateEmail(longEmail)).toBe(false); });});You review: “Those tests look right. Now implement it.”
Claude writes the function. You run the tests:
> Run the testsAll pass. Now you can confidently use this function knowing it’s verified.
Multi-Instance TDD
Section titled “Multi-Instance TDD”Use two Claude sessions (Module 1, Lesson 5):
Left pane: Claude writing tests based on your requirementsRight pane: Claude writing implementationThis creates a healthy separation — the test writer doesn’t see the implementation and vice versa, reducing the chance of circular assumptions.
Integration Testing
Section titled “Integration Testing”For API endpoints, Claude can write integration tests:
> Write integration tests for the POST /api/users endpoint.> It should create a user, return 201, and store it in the database.> Test error cases too: duplicate email, missing fields, invalid data.Exercise
Section titled “Exercise”- Describe a small feature (e.g., a password strength checker)
- Ask Claude to write tests first
- Review the tests — do they cover the right cases?
- Ask Claude to implement the function
- Run the tests and iterate if any fail