UGPL.net/blog
Posted on
Background

Better results from AI coding assistants with TDD

Author
Better results from AI coding assistants with TDD

Update in 26th of April 2026 found simonwillison.net
Initially found on theregister.com

Test driven development (TDD) produces much better results with AI coding assistants:

TDD prevents a failure mode where agents write tests that verify broken behavior. When the tests exist before the code, agents cannot cheat by writing a test that simply confirms whatever incorrect implementation they produced.

Here, for example, is the loop used within the so called red/green TDD:

  ┌────────────────────┐   ┌──────────────────┐   ┌───────────────────────┐
  │ 1. WRITE A LIST    ├───► 2. PICK ONE TEST ├───► 3. WRITE THE TEST     │
  │    OF TEST CASES   │   │    exactly one   │   │    red                │
  └────────▲───────────┘   └───────▲──────────┘   └──────────┬────────────┘
           │          imrpove      │          loop           │          
  ┌────────┴───────────┐   ┌───────┴──────────┐   ┌──────────▼────────────┐
  │ 5. ADD TO THE LIST ◄───┤ 5. REFACTOR      ◄───┤ 4. MAKE THE TEST PASS │
  │    as you learn    │   │    optionally    │   │    green              │
  └────────────────────┘   └──────────────────┘   └───────────────────────┘

This canonical red/green test driven development was developed as part of Extreme Programming (XP):

  1. Derive a list of the tests you want to cover from your requirements
  2. Pick up exactly one test from the list
  3. Turn this pick into an actual, concrete, runnable test ⇒ flag red (it should fail because implementation is not yet done)
  4. Change the code to make this test (and all previous tests) pass ⇒ flag green (because at the end the test should pass)
  5. Optionally refactor your old and newly added code to improve the implementation design
  6. Add items to the test list as you discover them
  7. Until the list is empty, go back to step 2

Read more about red/green TDD in