Building and refactoring with tests first
Claude Code examines the test files already present in a project to write a new test in the same style before the implementation, and confirming that this test genuinely fails proves it does indeed test something that does not exist yet.
Claude examines the test files already present in a repository to reproduce their style, their test framework, and their assertion conventions, rather than imposing a convention foreign to the project. The gesture consists of requesting these tests before writing the implementation they are meant to verify, then running the test runner to confirm they genuinely fail.
Confirm red before asking for green
This sequence, known in software engineering as red, green, refactor, does not depend on any Claude Code specific feature, it remains valid with any tool. Red proves that the test can fail, green proves that the implementation satisfies it, and jumping straight to green without having seen red leaves an awkward question open: whether the test would have passed even without any implementation at all.
The following example fabricates its own data to stay valid for any reader. A test expects a calculerRemise function to apply a ten percent discount, before this function even exists.
function testCalculerRemise() {
const resultat = calculerRemise(200);
console.assert(resultat === 180, "attendu 180, obtenu " + resultat);
}
testCalculerRemise();
// ReferenceError: calculerRemise is not defined, le rouge attendu
Once this red is observed, the minimal implementation is enough to turn it green.
function calculerRemise(prix) {
return prix * 0.9;
}
testCalculerRemise();
// aucune sortie : l'assertion est vraie, le test est au vert
Refactoring one concern at a time
The test runner stays active throughout the refactor that follows, and each change touches a single concern, one name renamed, one function extracted, never several at once in the same turn. This discipline isolates the regression: if the test runner fails after a change, the cause lies in that precise change, not in a stack of piled up modifications that would need to be untangled. Explicitly asking Claude to identify uncovered edge cases before considering a set of tests complete rounds out the picture, particularly for empty, negative, or out of range inputs that the initial implementation often ignores.
This requirement for executed proof echoes the regression test that closes a bug fix: in both cases, confidence comes from an output that was read, not from a reading of the code.
Red, green, refactor
A developer asks Claude Code to write a test for a discount calculation function before writing that function. She runs this test and reads the error message displayed in the terminal.
Write in one sentence what this situation establishes, and in one sentence what it does not establish.
What this establishes: This result establishes that the test was written and run before the function it is meant to verify existed, and that an error message was indeed produced at that moment.
What this does not establish: It does not establish that the implementation that follows will satisfy this test, since no calculerRemise function has been written yet.
The three most common miscalibrations
- Too broad This result proves that every test written by Claude Code in this project will systematically fail before the implementation.
- Too narrow This result shows nothing since only one error message in one terminal was observed.
- Beside the point This result shows that the terminal used by the developer displays error messages in colour.
- Claude examines the test files already present in the repository to reproduce their style and assertion conventions, rather than imposing a new one.
- Red proves that the test can fail and green proves that the implementation satisfies it, jumping straight to green leaves open the question of a test that would have passed without anything implemented.
- A refactor touches a single concern at a time, with the test runner active throughout, to isolate the cause of each regression.
- Explicitly asking for uncovered edge cases, empty, negative, or out of range inputs, rounds out a set of tests that the initial implementation often ignores.
Pick a small function you still need to write today, ask Claude Code to write the test first from your project's conventions, run that test to check that it genuinely fails, then write the minimal implementation that turns it green.
Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.
- Claude Code, common workflows, working with tests consultée le 2026-09-02