Course outline
Agentic Engineering: the hands-on course · Module 4: Verification loops
Tests as the agent's compass (TDD with agents)
An agent can only aim at what it can check. Write the failing test first, lock it, and let the agent iterate until it goes green without touching it.
Lesson 13 / 24 · ⏱ 8 min
An agent without a check is guessing, and it will tell you the guess worked. An agent with a failing test has a direction, a finish line and a way to notice its own mistakes.
That’s why I treat tests as the most important input to any agent run. More important than the prompt.
Why a red test beats a good prompt
A prompt describes what you want in words, and words are ambiguous. A failing test describes it as an exit code. The agent can run it, read the failure, change something and run it again, as many times as it needs, without you in the loop.
- RedA test that fails for the right reason
- LockYou review and commit the test
- Agent iteratesEdit, run, read failure, repeat
- GreenTest passes, test file untouched
- RefactorClean up with the suite as a net
↺ One behaviour per loop
Who writes the test?
You don’t have to write every test yourself. But you do have to own them. If the agent writes the test and the code in one go, a misunderstanding gets encoded twice, and the test happily confirms the bug.
✕ Agent writes test and code together
- –One prompt: 'add the feature with tests'
- –Tests are shaped around whatever the code ended up doing
- –Expected values copied from the implementation's output
- –Everything is green, nothing is proven
✓ Test first, then code
- +Agent (or you) drafts tests from the spec, nothing else
- +You read them, fix expected values, run them: red
- +You commit the tests before any implementation exists
- +Agent implements with 'don't modify the test files'
The brief I use
Two short runs. The first produces tests only; the second makes them pass.
Run 1 — tests only
Write failing tests for: cancelling an order refunds the full amount
when it's cancelled within 24 hours, and 50% after that.
Put them next to the existing order tests and follow their style.
Do not write or change any implementation code.
Stop when the tests run and fail because the behaviour is missing.
Run 2 — implementation
Make the tests in orders/cancel.test pass.
Do not modify, skip or delete any test. If you believe a test is
wrong, stop and explain why instead of changing it.
Done when: the full test suite passes.
For refactors, flip it: ask the agent first for characterization tests that pin down current behaviour, commit those, then refactor against them. We’ll see in lesson 4.4 what happens when a test is left unlocked.
Check yourself
🧠 Tests and agents
Score: 0 / 4
1. Why should you commit the failing tests before the agent implements anything?
2. Your new test fails with 'module not found'. What do you do?
3. What's the main risk of 'implement this and add tests' in one prompt?
4. You're about to have an agent refactor a module with no tests. First step?
Before you move on
✅ Key takeaways
0 / 5 completed