Use when implementing any new feature or function. Use when asked to "add tests later". Use when writing code before tests.
Write the test first. Watch it fail. Then write the code.
TDD is not about testing - it's about design. Writing tests first forces you to think about the interface before the implementation.
NEVER write production code without a failing test first.
No exceptions:
RED → GREEN → REFACTOR → REPEAT
If you're about to write implementation without a test, STOP:
// ❌ VIOLATION: Implementation first
function calculateDiscount(total: number): number {
return total > 100 ? total * 0.1 : 0;
}
// "We'll add tests later if needed"
// ✅ CORRECT: Test first
describe('calculateDiscount', () => {
it('returns 10% discount for orders over $100', () => {
expect(calculateDiscount(150)).toBe(15);
});
it('returns 0 for orders $100 or less', () => {
expect(calculateDiscount(100)).toBe(0);
expect(calculateDiscount(50)).toBe(0);
});
});
// NOW write the implementation
function calculateDiscount(total: number): number {
return total > 100 ? total * 0.1 : 0;
}
| Test-After | Test-First | |------------|------------| | Tests verify what code does | Tests define what code should do | | Implementation drives design | Requirements drive design | | Tests often skipped | Tests always exist | | Hard to test = poor design | Hard to test = caught early | | "Does it work?" | "Is it right?" |
Pressure: "Just write the function, we can test it later"
Response: "Later" never comes. Tests written after are weaker and often skipped entirely.
Action: Write the test now. It takes 30 seconds.
Pressure: "This function is trivial, testing is overkill"
Response: Simple functions grow complex. Simple bugs cause outages. Test everything.
Action: Write the test. Simple functions = simple tests.
Pressure: "I already have the solution in my head"
Response: TDD isn't about uncertainty - it's about capturing requirements as executable specs.
Action: Write the test first. Prove your mental model is correct.
Pressure: "Writing tests takes extra time"
Response: Debugging takes 10x longer than testing. TDD is faster overall.
Action: The test you write now saves hours of debugging later.
All of these mean: Stop. Write the test first.
| Situation | TDD Response | |-----------|--------------| | New feature | Write failing test → implement | | Bug fix | Write test that reproduces bug → fix | | Refactor | Ensure tests exist → refactor → tests pass | | "Tests later" | Tests now. Always now. |
| Excuse | Reality | |--------|---------| | "Tests slow me down" | Debugging slows you down more. | | "It's too simple" | Simple tests are fast to write. | | "I'll test later" | You won't. Test now. | | "I know it works" | Prove it with a test. | | "Tests are for QA" | Tests are for developers. | | "Just a prototype" | Prototypes become production. Test them. |
Test first. Code second. No exceptions.
The test is the specification. The test is the documentation. The test is the safety net. Write it first, every time.
npx skills add yanko-belov/test-driven-development下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer