~/academy $ claude learn tests-with-claude-code
Tests with Claude Code
Asking Claude Code for tests first, running them, fixing failures properly and making sure it never weakens a test to get a pass. 7 min
$ Read
Give Claude a check it can run
Claude stops when the work looks done. Without a check that comes back pass or fail, "looks done" is all it has, and you become the test suite. With one, it makes a change, runs the check, reads the failures and keeps going.
Put the exact test command in CLAUDE.md, including how to run a single test file. Then say what success means in the prompt: "run the billing tests after and keep going until they pass".
Always ask for evidence: the command it ran and the output. "All tests pass" on its own tells you very little.
Lesson notes: Tests with Claude Code
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You can run a test first loop with Claude Code and spot when a test has been quietly watered down.
Step 1 of 6 · Read
Give Claude a check it can run
Claude stops when the work looks done. Without a check that comes back pass or fail, "looks done" is all it has, and you become the test suite. With one, it makes a change, runs the check, reads the failures and keeps going.
Put the exact test command in CLAUDE.md, including how to run a single test file. Then say what success means in the prompt: "run the billing tests after and keep going until they pass".
Always ask for evidence: the command it ran and the output. "All tests pass" on its own tells you very little.
Step 2 of 6 · Read
Tests first
For a bug or a new feature, ask for the test before the fix. Claude writes a test that describes the behaviour you want, runs it and shows it failing. Only then does it change the code until the test passes.
This gives you proof the test can catch the problem, and a clear definition of done that does not depend on Claude's own opinion.
Ask it to commit the new tests on their own before it touches the implementation. Later, if the test file changes, you will see it in the diff straight away.
Step 3 of 6 · Improve a prompt
Ask for tests that mean something
Rewrite this prompt so Claude Code writes useful tests for an invoice total function before changing any code.
Starting prompt:
add tests for the invoice code
Hint: Name the file and function, list the cases that matter, point to existing tests for style, say how to run them, and say not to change the code yet.
Show a strong version
I am about to change our billing code, so first write tests for calculateTotal in src/billing/invoice.ts. Cover: VAT at 20%, an invoice with a zero value line, a discount larger than the subtotal (the total should be 0, never negative) and rounding to 2 decimal places. Follow the style of the existing tests in tests/billing/, for example the one for applyDiscount. Run them with npm test and show me the output as a list of passes and failures. Do not edit invoice.ts yet.
Step 4 of 6 · Try it in the terminal
Run the suite yourself
You want to see the test output with your own eyes, without asking Claude to run it. Use shell mode to run npm test straight from the Claude Code prompt.
Hint: Start the line with an exclamation mark, then the command.
Show the command
$ ! npm test > vitest run FAIL tests/billing/invoice.test.ts > discount larger than subtotal expected -12.5 to be 0 Tests 1 failed | 11 passed (12)
Step 5 of 6 · Read
Fix the code, not the test
When a test fails, the quickest way to a green result is to change the test. Say up front: "fix the code, not the tests. If you think a test is wrong, stop and explain why before changing it."
Then check the test files in
/diff. The warning signs are an expected value changed to match the output, an assertion deleted, a test marked as skipped, or a mock widened until anything passes.For extra safety while Claude fixes the implementation, add a temporary deny rule such as
Edit(./tests/**)so it cannot edit test files at all. Remove it when you want new tests written.Step 6 of 6 · Sort
Real fix or weakened test?
Claude says the failing test now passes. Sort what it did.
- Changed calculateTotal so the total never drops below zero
- Changed the expected value in the test from 0 to -12.5
- Marked the failing test as skipped
- Added a second test for a discount exactly equal to the subtotal
- Deleted the assertion that checks rounding
- Fixed a wrong VAT rate in the test data after explaining why and getting your agreement
Show the answers
- Changed calculateTotal so the total never drops below zero: Real fix. The code now does what the test describes. That is the goal.
- Changed the expected value in the test from 0 to -12.5: Weakened test. The test now describes the bug as correct behaviour. Reject it.
- Marked the failing test as skipped: Weakened test. A skipped test checks nothing. The suite is green and the bug is still there.
- Added a second test for a discount exactly equal to the subtotal: Real fix. Extra coverage of a boundary case makes the suite stronger.
- Deleted the assertion that checks rounding: Weakened test. Removing a check to get a pass is the classic way tests get watered down.
- Fixed a wrong VAT rate in the test data after explaining why and getting your agreement: Real fix. Tests can be wrong too. Changing one is fine when the reason is stated and you approve it.