Lesson 32 of 1570
Tests as Prompts — an Unexpected Superpower
Writing a test first is not just good engineering. It is the clearest possible prompt for an AI. Let's use tests to make AI code reliable.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1A Test Is an Executable Prompt
- 2test-driven development
- 3assertions
- 4red-green-refactor
Concept cluster
Terms to connect while reading
Section 1
A Test Is an Executable Prompt
A test says: when I give the function this input, I expect exactly this output. That is the most precise way to describe behavior that exists. If you paste a failing test and ask the AI to make it pass, you get amazingly focused results.
The red-green-refactor loop, with AI
- 1Write a test that fails (red)
- 2Ask the AI to write the function to make it pass (green)
- 3Check the code works and is readable
- 4Ask the AI to clean it up without breaking tests (refactor)
A concrete example in Python
Writing the tests first gives the AI an unambiguous target. No room for misinterpretation.
# Step 1: write the tests first
def test_add_positive():
assert add(2, 3) == 5
def test_add_negative():
assert add(-1, -1) == -2
def test_add_zero():
assert add(0, 42) == 42
# Step 2: paste the tests into your AI chat and say:
# "Write the `add` function so all three tests pass."
# Step 3: the AI almost always produces:
def add(a, b):
return a + b
# Step 4: run pytest. If green, ship it.Ask the AI to help write tests too
You do not need to write every test by hand. Give the AI the function signature, ask for test cases including edge cases, then review the list. You will often find a case you had not considered — that is the point.
Fast frameworks to know
Compare the options
| Language | Framework | Command |
|---|---|---|
| Python | pytest | pytest |
| TypeScript | Vitest | npx vitest |
| JavaScript | Jest | npm test |
| Go | testing (built-in) | go test ./... |
| Rust | cargo test | cargo test |
“Tests are the only prompt that runs.”
Key terms in this lesson
The big idea: a test is a prompt the machine can verify. Writing tests first turns every AI coding session into a tight loop: target, generate, check, done.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Tests as Prompts — an Unexpected Superpower”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 25 min
What Does AI-Assisted Coding Even Mean?
AI-assisted coding is not magic and not cheating. It is a new way of working where a model drafts, you decide. Let's draw a map before we start building.
Builders · 30 min
Your First Copilot-Style Completion
Let's actually feel what autocomplete is like. Write a comment, pause, and watch a full function appear. Then learn what to do next.
Builders · 30 min
Prompting for Code Is Different From Prompting for Prose
A prompt that writes a poem is not the same as a prompt that ships working code. Code has hidden standards. You need to make them explicit.
