Track lessons
AI-Era Engineering
0 of 2 completed
- Spec-Driven Development: Write Specs AI Agents Can Execute
- Agentic TDD: Make the AI Write Failing Tests First
Other tracks
Agentic TDD: Make the AI Write Failing Tests First
An agent can write implementation and tests that agree with the same mistaken assumption. Writing and reviewing the failing tests first separates the behavioral contract from the code that must satisfy it.
When to use
- A rule has crisp edge cases such as expiry, reuse or authorization.
- You are fixing a regression and can reproduce it as a test.
- You want to stop an agent from weakening tests to make its implementation pass.
Example
Specify that a reset token expires after 30 minutes and works once. Confirm both tests fail, commit them, then let the agent change only production code.
TL;DR
Tests first, confirm they fail, commit them, then forbid the agent from editing them.
Write tests for the behavior below. Do not implement. Run them and confirm they fail.Steps
- 1
Tests only
Describe behavior and edge cases, name the destination test file, and explicitly forbid placeholder implementations so the first run proves the tests can fail.
promptWe are doing TDD. Write tests in src/auth/reset.test.ts for: - token expires after 30 minutes - token works once Do not create mock implementations. Run the tests and confirm they fail. - 2
Lock them in a commit
Review the failures yourself, then commit the accepted tests as a stable boundary before implementation starts.
bashgit add src/auth/reset.test.ts git commit -m "test(auth): reset token rules" - 3
Implement
Give the agent one constraint that matters most: production code may change, but the approved tests may not.
promptMake the tests pass. Do not modify any test file. Run tests until green. - 4
Verify the tests were not touched
Compare test files against the tests-only commit; an empty result confirms implementation did not rewrite the contract.
bashgit diff HEAD~1 --stat -- '*.test.ts'
Gotchas
- Agents love editing tests to pass; the commit + diff check catches it.
- A test that passed before implementation tests nothing.
- Ask for edge cases explicitly: expiry, reuse, wrong user.
- Review the tests yourself before step 3; they are the spec.
Cheat sheet
| 1. Tests | Write, run, see red |
| 2. Commit | Freeze the tests |
| 3. Implement | No test edits allowed |
| 4. Verify | git diff on test files |
Related
Sources
Reviewing agent output? CodeCrab reviews pull requests locally with your own AI tools.
