Loading...
Loading...
Compare original and translation side by side
WRONG (horizontal): RED: t1 t2 t3 t4 GREEN: i1 i2 i3 i4
RIGHT (vertical): t1→i1 t2→i2 t3→i3 ...WRONG (horizontal): RED: t1 t2 t3 t4 GREEN: i1 i2 i3 i4
RIGHT (vertical): t1→i1 t2→i2 t3→i3 ...test(red): | Language | Marker | Strict? |
|---|---|---|
| Python (pytest) | | yes — XPASS fails suite |
| TS/JS (vitest) | | yes |
| TS/JS (jest) | | yes |
| Go | | aggregate only |
| Rust | | aggregate only |
| Other | find a strict expected-failure mechanism; none exists → commit unmarked, tell the human the repo lacks red enforcement |
red-teststest(red): | 编程语言 | 标记 | 是否严格? |
|---|---|---|
| Python (pytest) | | 是 —— XPASS会导致测试套件失败 |
| TS/JS (vitest) | | 是 |
| TS/JS (jest) | | 是 |
| Go | | 仅聚合 |
| Rust | | 仅聚合 |
| 其他 | 寻找严格的预期失败机制;如果不存在 → 不添加标记提交,并告知用户该仓库缺少red状态的强制机制 |
red-testsfeat: fix: feat: fix: lint-red.shstagedcommittest(red):mergered-testslint-red.shstagedcommittest(red):mergered-tests| Quality | Rule |
|---|---|
| Behavioral | Exercises a real path through the public API; survives refactors |
| Minimal | One thing. "and" in the name? Split it. |
| Clear | Name states the behavior, not |
| Honest | Tests the code, never the mock |
// Good — tests real behavior (vitest; jest: it.failing)
test.fails('retries failed operations 3 times', async () => {
let attempts = 0;
const op = () => { attempts++; if (attempts < 3) throw new Error('fail'); return 'ok'; };
expect(await retryOperation(op)).toBe('ok');
expect(attempts).toBe(3);
});| 特性 | 规则 |
|---|---|
| 行为导向 | 测试公共API的真实路径;在重构后仍能正常运行 |
| 最小化 | 只测试一件事。名称里有“and”?拆分它。 |
| 清晰性 | 名称说明行为,而非 |
| 真实性 | 测试代码,而非模拟对象 |
// 优质示例 —— 测试真实行为(vitest;jest使用it.failing)
test.fails('retries failed operations 3 times', async () => {
let attempts = 0;
const op = () => { attempts++; if (attempts < 3) throw new Error('fail'); return 'ok'; };
expect(await retryOperation(op)).toBe('ok');
expect(attempts).toBe(3);
});| Excuse | Reality |
|---|---|
| "Too simple to test" | Simple code breaks. The test takes 30s. |
| "I'll test after" | Tests-after are biased by the implementation: "what does this do?" not "what should this?" |
| "Deleting hours of code is wasteful" | Sunk cost. Unverified code is debt. |
| "Test is hard to write" | Hard to test = hard to use. Listen to it; simplify the interface. |
| "Must mock everything" | Too coupled. Inject dependencies. |
| "Lint passed, TDD done" | Hooks check structure, not that you watched the failure. |
| 借口 | 真相 |
|---|---|
| “太简单了,没必要测试” | 简单代码也会出错。写测试只需要30秒。 |
| “我之后再测试” | 事后编写的测试会受实现影响:关注的是“这段代码做了什么?”而非“这段代码应该做什么?” |
| “删除数小时的代码太浪费了” | 沉没成本谬误。未验证的代码就是技术债务。 |
| “测试太难写了” | 难测试 = 难使用。倾听这个信号;简化接口。 |
| “必须模拟所有东西” | 耦合度过高。注入依赖项。 |
| “Lint通过了,TDD就完成了” | 钩子只检查结构,不检查你是否观察到了测试失败。 |