Skip to main content

Test-First with an Agent (TDD)

Why This Matters

Tests written after the code by the same agent tend to confirm what the code does, including its bugs. Writing the test first, and checking that it fails for the right reason, turns the test into a specification the agent has to meet instead of a mirror of its own work.

The TDD loop with an agent as driver

  1. You describe the behaviour (or point to the spec).
  2. The agent writes the tests first and runs them.
  3. You check they fail for the right reason: missing behaviour, not a typo or import error.
  4. The agent writes the minimal code to make them pass, without touching the tests.
  5. Refactor with the tests as a safety net.

Real Vitest output from the example project: six passRate and formatPassRate tests fail with Error: Not implemented, then the same six tests pass after the implementation, with the tests unchanged

This is the real run from the solution branch solution/lesson-13-pass-rate-tdd in TestRun Board; its docs/solutions/lesson-13-pass-rate-tdd.md contains the full output and the prompts. The red run fails for the right reason: the behaviour does not exist yet, not a typo or an import error. The key check: the tests did not change between red and green. Make that explicit in your prompt:

Step 1: write tests for summariseHistory() covering stable, flaky, broken and
empty histories. Run them and show me the failures. Do not write the function yet.

Step 2 (after my OK): implement the minimal code to make them pass.
Do not modify any file under tests/.

Tests that prove behaviour versus tests that please the model

Proves behaviourPleases the model
expect(passRate).toBe(0.7)expect(result).toBeDefined()
Fixture with a known history and known outcomeOutput of the function copied into the expectation
Named after the behaviour: “returns 0 for empty history”Named after the function: “summariseHistory works”
Fails when you break the code on purposeStill passes when you break the code

The last row is a test you can run yourself: change one line of the implementation (flip a comparison, return a constant) and see whether a test fails. Mutation testing automates exactly that (lesson 21, Regression Strategy and Governance Basics).

When tests first, and when not

Test first when the behaviour is clear and has logic: calculations, filters, parsing, state transitions, bug fixes (write the failing test that reproduces the bug). Explore first when you do not know the shape yet: a UI prototype or a spike against an unknown API. Then throw the spike away and redo it test-first, or add tests before merging.

Hands-on

  1. Pick a pure function you need (or summariseHistory from the example).
  2. Run the two-step prompt above. Confirm the red run fails for the right reason.
  3. After green, break the implementation on purpose. Does a test catch it?
  4. Add a rule to CLAUDE.md: “When asked for TDD, never modify tests in the green step.”

Walkthrough in TestRun Board

  1. Start from main on a new branch and send step 1 of the prompt above for passRate() and formatPassRate() (the rules are in docs/specs/pass-rate-column.md: one decimal, “n/a” below 3 runs, the latest 20 runs).
  2. Check the red run. It must fail with Error: Not implemented (missing behaviour), not with a syntax or import error.
  3. Save a copy of the tests (cp tests/lib/history.test.ts /tmp/before.ts), send step 2, and afterwards check diff /tmp/before.ts tests/lib/history.test.ts. It must print nothing.
  4. Break it on purpose: change < MIN_RUNS_FOR_PASS_RATE into <=. A test must fail. Then let the tool do this for you: git commit your work and run npm run mutation:changed -- main. The worked solution scores 100% on src/lib/history.ts.
  5. Compare with solution/lesson-13-pass-rate-tdd and read docs/solutions/lesson-13-pass-rate-tdd.md for the prompts used.

Key Takeaways

  • Test first makes the test a specification instead of a mirror.
  • Check that red fails for the right reason, and that tests do not change on the way to green.
  • Precise assertions and known fixtures prove behaviour; vague ones do not.
  • Break the code on purpose to check your tests are worth something.