Failing test first
Red, green, one small step at a time. Test behavior a user can observe, not the implementation.
SKILL.mdmarkdown · 29 lines
---
name: tdd
description: >
Test first: write a failing test, then the implementation, one small step at a time.
Use for new features and bug fixes, or when the user mentions "test first", "TDD", or "red-green".
Not for docs, config, or styling changes.
---
# Write the failing test first
## One round
1. Red: write one test that describes a behavior a user can observe. Run it and watch it fail because the feature isn't there yet.
2. Green: write the least code that makes it pass. Don't build the next feature while you're there.
3. Run [test command] and confirm nothing else broke.
4. Commit, then start the next round.
## What a good test looks like
- It goes through the public interface: a function signature, an HTTP endpoint, a component's visible output. No private methods.
- Its name reads like a requirement: "an empty cart can't check out".
- The expected value comes from the spec or a worked example, not from rewriting the implementation.
- It survives a refactor. If behavior didn't change, the test shouldn't go red.
## Fixing a bug
Write a test that reproduces the bug, watch it fail, then fix. Keep the test as the regression test.
## Don't
- Don't write all the tests first and all the code after.
- Don't mock things inside your own module. Mock only what's outside the process: network, time, third-party services.
- Don't change an assertion to get to green. Work out first whether the code is wrong or the requirement changed.