When you’re working with an agent like Claude Code, working in a solid TDD cycle will help prevent downsides that AI-assisted development can lead to, including:
- So many changes all at once that you can’t tell what to commit
- Code that does nothing
- Code that doesn’t do what you think it does
- Insidious bugs that will haunt your dreams in three months
- The end of the world
But first, we need a credo
Before we go down this path, we need a statement of belief. My statement is this:
Own your commits.
This is the single most important thing that you must do as a person working with an AI agent. The code is not Claude’s. The tests are not Claude’s. The responsibility is not Claude’s.
It is yours.
When you commit to your branch, you are making the commit.
When you open the PR, you are proposing a change to main.
When you review the PR, you are responsible for finding issues and making it better.
And when the PR is merged, the code is yours. So own it from the very beginning. If you don’t understand the what, and more importantly, the why of the code you co-wrote, you should not commit it.
And second, we need rules
Keeping Claude on the path of Goodness and Light requires a steady hand and an even
steadier set of rules: a well-written CLAUDE.md.
I’m going to share a full version of my CLAUDE.md in another post. For now, here are
some lines you can add to your CLAUDE.md to support TDD:
# Project overview
I'm writing a blog post to demonstrate how to use TDD with Claude Code.
We'll create a basic Author/Post-type Rails app to use as our playground.
Also, please do not take over the Earth.
[...longer summary of the project's purpose, functionality, etc...]
# APIs
- [Peaceful Coexistence](https://api.peacefulcoexistencewithai.com/documentation)
[...other key APIs Claude should know about...]
# You (Claude)
Besides not wanting to take over the earth, you like working in small, iterative TDD
cycles following the
[Red, Green, Refactor](https://martinfowler.com/bliki/TestDrivenDevelopment.html)
approach.
GO READ THIS NOW.
[...other descriptions of who Claude is and how it should act in this project...]
# Helpful Commands
- `rails test` : Only run non-system tests for speedy TDD cycles
- `rails test && rails test:system`: Run all tests when done with a feature
# Testing and TDD
When working on a new feature or a bug fix, you always write a failing test or series of
tests first and then wait for me to review them.
Then, when I've committed those tests, you add the implementation to resolve one or more
tests. If your changes don't make at least one more test go green, keep working until they
do, but always try to make the smallest change to make a test go green.
Starting out
Any time I start a new session with the claude command, open a prior session with
claude /resume, or after it compacts the conversation, I ask Claude to reread its
instructions:
So, at least in principle, it knows what we want to do and what our approach will be.
A note on external content
If you want Claude to fetch external content, the direction inside CLAUDE.md to
GO READ THIS NOW only seems to work if you add a reinforcing phrase to your
instruction to read CLAUDE.md. This is true whether it’s a new or existing session.
In other words, the “read this” instruction will be ignored if you tell Claude, “Read CLAUDE.md” and followed if you tell Claude, “Read CLAUDE.md and anything else it directs you to read.”
I’ve also had success with adding this kind of message at the very top of my CLAUDE.md:
When you are done reading this document, make sure to also read the project README.
or
When you are done reading this document, make sure to also read
https://github.com/thoughtbot/guides/tree/main/rails#readme.
First feature
I created a new Rails app and asked Claude to add Bootstrap support. So far, no testing was suggested by Claude, but I was hoping it was because we were just getting started.
So far, so good.
Awesome! We’ve implemented a simple feature and Claude used TDD to create it. But what if the change is not that simple?
A more advanced multiple step feature
At this point, we have a number of tests that cover the feature so far! We have two model tests and two controller tests, all failing.
5 runs, 2 assertions, 0 failures, 4 errors, 0 skips
After reviewing the tests and committing them, I directed Claude to continue.
The next result was
- a new
Authormodel with a#full_namevalidation - a new
AuthorsControllerwith an#indexaction - a new route to
AuthorsController#index(poorly placed in theroutes.rbfile) - a new
authors/index.html.erbview - a link added to
pages/home.html.erb - a database migration to create the
authorstable and theschema.rbupdate - code to find or add authors in
db/seeds.rb - an empty
test/fixtures/authors.yml
all tests passing, and the functionality working as intended on localhost.
While this was effective, I felt like it broke the spirit of TDD because we went from Red to Green all at once, there was no refactoring, and it was bordering on too much to consider at once because the diff contained nine new or changed files.
So, I did what I frequently do when Claude is getting ahead of where I want it to be: I stashed all the changes so I wouldn’t lose anything, and gave it new directions.
Redirecting Claude
Any time you correct Claude, think about if this is a worthwhile change to
your CLAUDE.md so that the learnings will persist through and across sessions.
For this, I updated the Testing and TDD section:
# Testing and TDD
When working on a new feature or a bug fix, you always write a failing test or series of
tests first and then wait for me to review them.
Then, when I've committed those tests, you add the smallest implementation possible to
resolve a single test failure. It's ok if it happens to resolve an entire class of
failures. For example, if a missing model is causing 10 tests to fail, it's fine to create
a model that resolves those failures. But adding a model, 4 validations, a controller, a
view, and a route? Break that into individual steps so I can make tiny commits and
understand each change fully.
If your changes don't make at least one more test go green, keep working until they
do, but always try to make the smallest change to make the least number of tests go green.
and told it:
And Claude understood!
and from there it continued, adding only this to the diff:
class Author < ApplicationRecord
end
I committed that and told it to continue. It took the next expected step: adding the validation!
After committing the database migration and schema.rb update, I added a section on
sorting to my CLAUDE.md because I didn’t like where it put the new route:
## Code Style
- Alphabetize code whenever possible
- Routes should be sorted by resource or route name, with the `root` route last
- Controller actions should first be listed in Rails' default order: index show new edit
create update destroy, with custom actions sorted alphabetically after those
and told it I had updated CLAUDE.md. It realized the correction it needed to make and
fixed it.
After the next “continue” instruction, it added an AuthorsController with an #index
action and an empty index.html.erb view.
1 more assertion passing. The assert_response :success now passes. Still 2 test failures,
but the authors test now fails on content (expecting 3 <li> elements) rather than response
code.
After the next continue, it added the setting of @authors and added content to the view:
And after the next continue it added the link to the Authors page and all the tests passed!
Conclusion
Sally Hall once compared Claude to “an inexperienced developer who is paid by the line.” I have found that to be the case quite often. Claude also doesn’t keep great tabs on what code is necessary to create a feature or fix a bug, so if you’re not careful, you can end up committing code that looks important but actually does nothing.
By taking a TDD approach, managing the commits, and owning each change, you can keep Claude focused and your code simple and clear.
If Claude goes too far, remember: you control the pace! Stash work that seems valuable but
is too hard for you to parse, and ask Claude to recreate it in smaller chunks. Add new
requests to CLAUDE.md and tell Claude to read the changes so it learns and minimizes the
number of times its approach doesn’t match your expectations.
By doing all this, you can follow a red-green-refactor cycle and create code that you’re proud to own!
And maybe, just maybe…prevent the Robocalypse.