test-driven-development
它能做什么
它指导 AI 智能体采用先写测试的方式开发软件,在编写代码之前先为其编写测试。它适合希望代码能正常运行,并且能证明每项更改确实达到预期的开发者。
适用场景
- ✓你要求在代码中新增功能、修复错误或更改行为的时候。
- ✓你想在有测试保障的前提下安全地重构代码的时候。
- ✓需要用一个在更改前失败过的测试来证明某个错误已被修复的时候。
技能链接
https://github.com/obra/superpowers/blob/main/skills/test-driven-development/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请添加一个功能,让用户可以把列表导出为 CSV,并先编写测试。”
每周 5 个新技能,1 分钟读完。
工作原理
技能为新的行为编写测试,运行它以确认失败,然后编写所需的最少代码使其通过。接着,它会在保持测试通过的前提下整理代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Test-Driven Development (TDD)
Overview
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
When to Use
Always:
- New features
- Bug fixes
- Refactoring
- Behavior changes
Exceptions (ask your human partner):
- Throwaway prototypes
- Generated code
- Configuration files
Thinking "skip TDD just this once"? Stop. That's rationalization.
The Iron Law
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
- Don't keep it as "reference"
- Don't "adapt" it while writing tests
- Don't look at it
- Delete means delete
Implement fresh from tests. Period.
Red-Green-Refactor
digraph tdd_cycle {
rankdir=LR;
red [label="RED\nWrite failing test", shape=box, style=filled, fillcolor="#ffcccc"];
verify_red [label="Verify fails\ncorrectly", shape=diamond];
green [label="GREEN\nMinimal code", shape=box, style=filled, fillcolor="#ccffcc"];
verify_green [label="Verify passes\nAll green", shape=diamond];
refactor [label="REFACTOR\nClean up", shape=box, style=filled, fillcolor="#ccccff"];
next [label="Next", shape=ellipse];
red -> verify_red;
verify_red -> green [label="yes"];
verify_red -> red [label="wrong\nfailure"];
green -> verify_green;
verify_green -> refactor [label="yes"];
verify_green -> green [label="no"];
refactor -> verify_green [label="stay\ngreen"];
verify_green -> next;
next -> red;
}
RED - Write Failing Test
Write one minimal test showing what should happen.
<Good> ```typescript test('retries failed operations 3 times', async () => { let attempts = 0; const operation = () => { attempts++; if (attempts < 3) throw new Error('fail'); return 'success'; };const result = await retryOperation(operation);
expect(result).toBe('success'); expect(attempts).toBe(3); });
Clear name, tests real behavior, one thing
</Good>
<Bad>
```typescript
test('retry works', async () => {
const mock = jest.fn()
.mockRejectedValueOnce(new Error())
.mockRejectedValueOnce(new Error())
.mockResolvedValueOnce('success');
await retryOperation(mock);
expect(mock).toHaveBeenCalledTimes(3);
});
Vague name, tests mock not code </Bad>
Requirements:
- One behavior
- Clear name
- Real code (no mocks unless unavoidable)
Verify RED - Watch It Fail
MANDATORY. Never skip.
npm test path/to/test.test.ts
Confirm:
- Test fails (not errors)
- Failure message is expected
- Fails because feature missing (not typos)
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
GREEN - Minimal Code
Write simplest code to pass the test.
<Good> ```typescript async function retryOperation<T>(fn: () => Promise<T>): Promise<T> { for (let i = 0; i < 3; i++) { try { return await fn(); } catch (e) { if (i === 2) throw e; } } throw new Error('unreachable'); } ``` Just enough to pass </Good> <Bad> ```typescript async function retryOperation<T>( fn: () => Promise<T>, options?: { maxRetries?: number; backoff?: 'linear' | 'exponential'; onRetry?: (attempt: number) => void; } ): Promise<T> { // YAGNI } ``` Over-engineered </Bad>Don't add features, refactor other code, or "improve" beyond the test.
Verify GREEN - Watch It Pass
MANDATORY.
npm test path/to/test.test.ts
Confirm:
- Test passes
- Other tests still pass
- Output pristine (no errors, warnings)
Test fails? Fix code, not test.
Other tests fail? Fix now.
"Other tests" means the project's suite, not just your file. A
green run of the test you wrote is not a green suite. Before you call
the change done, run the project's test command (bare pytest,
npm test, cargo test — whatever the repo uses) even when your task
named only one test file. A scope statement in your task bounds the
deliverable, not your verification. Any failure that run shows —
including one you didn't cause — goes in your report by name; a red
test you watched scroll past and didn't mention is a report falsified
by omission.
REFACTOR - Clean Up
After green only:
- Remove duplication
- Improve names
- Extract helpers
Keep tests green. Don't add be
常见问题
做快速原型时可以跳过测试吗?
一次性原型和生成的代码属于例外,但跳过测试之前,技能会先询问你。
如果代码是在测试之前写的怎么办?
技能认为这种代码应当删除,并根据测试重新编写,以确保测试真正主导设计。
为什么要看着测试失败?
一个从未见过失败的测试,可能并没有测试到正确的内容。
详情
作者的原始描述
Use when implementing any feature or bugfix, before writing implementation code
repo: obra/superpowers
热度
- GitHub 星标
- 311k
- 排名
- #95
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
brainstorming
311k帮你的智能体在构建任何新东西之前,先通过有针对性的问题和清晰的方案,弄清楚你真正想要什么。
executing-plans
311k帮你的智能体在当前会话中,按步骤执行已写好的实施计划,每一步完成后先测试,再进入下一步。
finishing-a-development-branch
311k帮你的智能体收尾已完成的代码:先运行测试,再提供合并、发起拉取请求或保留分支等清晰选项。
receiving-code-review
311k帮你的智能体认真评估代码审阅意见,在改动任何内容之前,先对照代码逐条核实。