Lesson 7 / 25

Tests First, Code Second

Write or request tests before the implementation so correctness is checkable.

Give the assistant a target

The most reliable way to use an assistant is to define success first. Write a handful of tests (or ask the assistant to propose them and review them yourself), then ask for the implementation. If the tests pass, you have evidence; if they fail, paste the failure back and iterate. This also makes agents far more effective, because they can run the tests and fix their own mistakes.

Tests and an implementation, run

I ran this. Each of the four test cases from the prompt passes, which gives real evidence the function does what was asked.

import re

def slugify(text):
    text = text.strip().lower()
    text = re.sub(r"[^a-z0-9]+", "-", text)
    return text.strip("-")

tests = [("Hello World", "hello-world"), ("  Python 3.12!  ", "python-3-12"),
         ("", ""), ("--a--b--", "a-b")]
for given, want in tests:
    got = slugify(given)
    print(repr(given), "->", repr(got), "OK" if got == want else "FAIL")

Output:

'Hello World' -> 'hello-world' OK
'  Python 3.12!  ' -> 'python-3-12' OK
'' -> '' OK
'--a--b--' -> 'a-b' OK

Read the tests the assistant writes

An assistant may write tests that simply mirror its own code, including its mistakes. Check that each test would fail if the function were wrong; the testing section shows how.

Quick check: What is the benefit of writing tests before asking for code?

  • Success becomes checkable and the assistant has a target
  • Tests make code longer only
  • Assistants refuse to code otherwise
  • It removes the need to read code
Answer

Success becomes checkable and the assistant has a target — Tests define "correct" up front, so you verify results with evidence rather than impressions.