CONCEPT · TOOLS
Testing as Spec With Claude Code
How to ask Claude Code for tests that read like a human spec and actually validate behavior, instead of just passing.
2 min read · updated 2026-07
BEFORE READING
Context
Claude Code can generate 50 tests in seconds. The problem isn't quantity: it's whether those tests validate real behavior and read like a specification, not mechanical code that always passes. Using Claude Code for testing means steering the generation with judgment, not delegating the judgment.
Setup
- Have the test runner configured in the project (Vitest, Jest, Pytest, whatever you use) before you start.
- Give Claude Code context: which function, what behavior you expect, which runner. The test command and conventions ideally live in
CLAUDE.md. - Golden rule (from Claude Code best practices): give it a way to verify its work. Have it run the tests and read the result, not just make them "look" right.
State as of July 2026. Claude Code evolves; commands and flags may change.
Examples
A prompt that produces tests-as-spec:
Generate tests for canUserCheckout(user, cart) with Vitest.
Make each test read like a business sentence, not "test 1, test 2".
Cover: verified / not verified, with / without pending orders,
empty cart, user with no credit limit.
Then run the suite and show me the output.Expected result — the names are the spec:
describe('canUserCheckout', () => {
it('allows checkout when verified and no pending orders', ...)
it('blocks checkout when the user is not verified', ...)
it('blocks checkout when there is an unpaid pending order', ...)
})If Claude Code hands you "100 passing tests" but none break when you deliberately break the code, they're worthless. Ask it to make the tests fail when a bug is introduced, as proof they validate something.
Particularities
- Combines with
edge-cases-with-ai: first you ask it to discover the cases, then to turn them into tests. Discovering and covering are two distinct steps. - Real verification: require it to run the suite and show the output. "Looks done" isn't done — it's the central principle of Claude Code best practices.
- Coverage ≠ quality: a high coverage number with tests that assert nothing is smoke. Prioritize critical behavior over percentage.
- Limitation: Claude Code writes correct tests, but you decide WHAT is worth testing. That judgment isn't delegated.