Tooluck logoTooluck
SKILL.md

test-driven-development

Agent skill fromjnMetaCode/superpowers-zh

在实现任何功能或修复 bug 时使用,在编写实现代码之前

How to install test-driven-development

install test-driven-development
# install the superpowers-zh pack with the skills CLI
npx skills add jnMetaCode/superpowers-zh

# or add only this skill to Claude Code: copy the folder

skills/test-driven-development/

# into

~/.claude/skills/test-driven-development/ (all projects)

.claude/skills/test-driven-development/ (one project)

skills/test-driven-development/SKILL.md

MITFull file
1# 测试驱动开发(TDD)
2
3## 概述
4
5先写测试。看它失败。写最少的代码让它通过。
6
7**核心原则:** 如果你没有看到测试失败,你就不知道它是否测试了正确的东西。
8
9**违反规则的字面意思就是违反规则的精神。**
10
11## 何时使用
12
13**始终使用:**
14- 新功能
15- Bug 修复
16- 重构
17- 行为变更
18
19**例外(需询问你的人类伙伴):**
20- 一次性原型
21- 生成的代码
22- 配置文件
23
24想着"就这一次跳过 TDD"?停下来。那是在给自己找借口。
25
26## 铁律
27
28```
29没有失败的测试,就不写生产代码
30```
31
32先写了代码再写测试?删掉它。从头来过。
33
34**没有例外:**
35- 不要保留作为"参考"
36- 不要在写测试时"改编"它
37- 不要看它
38- 删除就是删除
39
40从测试出发,重新实现。句号。
41
42## 红-绿-重构
43
44```dot
45digraph tdd_cycle {
46 rankdir=LR;
47 red [label="红灯\n编写失败的测试", shape=box, style=filled, fillcolor="#ffcccc"];
48 verify_red [label="验证正确失败", shape=diamond];
49 green [label="绿灯\n最少代码", shape=box, style=filled, fillcolor="#ccffcc"];
50 verify_green [label="验证通过\n全部绿灯", shape=diamond];
51 refactor [label="重构\n清理代码", shape=box, style=filled, fillcolor="#ccccff"];
52 next [label="下一个", shape=ellipse];
53
54 red -> verify_red;
55 verify_red -> green [label="是"];
56 verify_red -> red [label="错误的\n失败"];
57 green -> verify_green;
58 verify_green -> refactor [label="是"];
59 verify_green -> green [label="否"];
60 refactor -> verify_green [label="保持\n绿灯"];
61 verify_green -> next;
62 next -> red;
63}
64```
65
66### 红灯 - 编写失败的测试
67
68写一个最小的测试来展示期望行为。
69
70<Good>
71```typescript
72test('retries failed operations 3 times', async () => {
73 let attempts = 0;
74 const operation = () => {
75 attempts++;
76 if (attempts < 3) throw new Error('fail');
77 return 'success';
78 };
79
80 const result = await retryOperation(operation);
81
82 expect(result).toBe('success');
83 expect(attempts).toBe(3);
84});
85```
86名称清晰,测试真实行为,只测一件事
87</Good>
88
89<Bad>
90…

Excerpt shown under the repository's MIT license.

More skills in superpowers-zh

Keep exploring

Similar skill repositories

Newsletter

Join the Community

Subscribe to our newsletter for the latest news and updates