test-driven-development
Use when implementing any new feature or function. Use when asked to "add tests later". Use when writing code before tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when implementing any new feature or function. Use when asked to "add tests later". Use when writing code before tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Use when writing tests. Use when test structure is unclear. Use when arrange/act/assert phases are mixed.
Use when designing or modifying APIs. Use when adding breaking changes. Use when clients depend on API stability.
Use when implementing authentication. Use when storing passwords. Use when asked to store credentials insecurely.
Use when same data is fetched repeatedly. Use when database queries are slow. Use when implementing caching without invalidation strategy.
Use when tempted to use class inheritance. Use when creating class hierarchies. Use when subclass needs only some parent behavior.
Use when acquiring multiple locks. Use when operations wait for each other. Use when system hangs without crashing.
基于 SOC 职业分类
| name | test-driven-development |
| description | 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.