Loading...
Loading...
Compare original and translation side by side
Describe the behavior in a test → Make it real → Clean up用测试描述行为 → 实现该行为 → 代码清理scripts/detect_test_env.sh.test.ts.spec.ts_test.gotest_*.pypackage.jsonMakefilenpm testscripts/detect_test_env.sh.test.ts.spec.ts_test.gotest_*.pypackage.jsonMakefilenpm testreferences/legacy-mode.mdreferences/legacy-mode.mdGOOD (behaviors): BAD (implementation steps):
- adds two positive numbers - create Calculator class
- returns 0 for 0 + 0 - implement add() method
- handles negative results - add validation logic
- rejects non-numeric input - handle edge casesreferences/test-case-derivation.md示例(良好的行为描述): 反面示例(实现步骤描述):
- 支持两个正数相加 - 创建Calculator类
- 0+0返回0 - 实现add()方法
- 处理负数结果 - 添加验证逻辑
- 拒绝非数字输入 - 处理边缘情况references/test-case-derivation.mdWRONG (horizontal): test1, test2, test3 → impl1, impl2, impl3
RIGHT (vertical): test1→impl1 → test2→impl2 → test3→impl3错误方式(横向推进): test1, test2, test3 → impl1, impl2, impl3
正确方式(垂直切片): test1→impl1 → test2→impl2 → test3→impl3references/test-quality.mdreferences/test-quality.mdundefinedundefined
```typescript
// TypeScript: create calculator.ts
export function add(a: number, b: number): number {
return undefined as any;
}// Go: create calculator.go
func Add(a, b int) int {
return 0
}
```typescript
// TypeScript: 创建calculator.ts
export function add(a: number, b: number): number {
return undefined as any;
}// Go: 创建calculator.go
func Add(a, b int) int {
return 0
}✗ Expected 5 but received 0
✗ Expected "confirmed" but received undefined
✗ Expected function to throw but it did not✗ Cannot find module './calculator'
✗ TypeError: add is not a function
✗ SyntaxError: Unexpected token✗ 预期为5,但实际得到0
✗ 预期为"confirmed",但实际得到undefined
✗ 预期函数抛出异常,但未抛出✗ 找不到模块'./calculator'
✗ 类型错误:add不是函数
✗ 语法错误:意外的标记Test: expect(add(2, 3)).toBe(5)
Code: return 5; ← literally this测试:expect(add(2, 3)).toBe(5)
代码:return 5; ← 就写这一行references/design-and-refactoring.mdreferences/design-and-refactoring.mdreferences/mocking-guidelines.mdreferences/mocking-guidelines.md[ ] Test describes behavior, not implementation
[ ] Test uses public interface only
[ ] Assertion executed and failed with WRONG VALUE (not import/type error)
[ ] Wrote minimal code to make test pass (Fake It / Triangulation / Obvious)
[ ] ALL tests pass (including pre-existing)
[ ] No speculative features added
[ ] Reported result AFTER GREEN, not after RED[ ] 测试描述的是行为,而非实现细节
[ ] 测试仅使用公共接口
[ ] 断言代码已执行且因结果错误失败(而非导入/类型错误)
[ ] 编写了最少的代码让测试通过(伪造实现/三角化/显而易见的实现)
[ ] 所有测试都通过(包括已有的测试)
[ ] 未添加投机性功能
[ ] 在GREEN阶段后再向用户汇报结果,而非RED阶段[ ] Every behavior has a test that was seen failing (assertion failure) first
[ ] Edge cases and error paths covered
[ ] All tests pass with clean output
[ ] Tests run independently (no order dependency)
[ ] Test names read as behavior specifications[ ] 每个行为都有先失败(断言失败)的测试覆盖
[ ] 覆盖了边缘情况和错误路径
[ ] 所有测试都通过,输出清晰
[ ] 测试可独立运行(无顺序依赖)
[ ] 测试名称可作为行为规范阅读| Problem | Solution |
|---|---|
| Don't know how to test | Write the API you wish existed. Assert first. Ask user. |
| Test too complicated | Design too coupled. Simplify the interface. |
| Must mock everything | Code too coupled. Use dependency injection. |
| Test passes immediately | Strengthen assertions. Verify it tests NEW behavior. |
| Import error on first run | Create stub file/function first, then re-run. |
| Tempted to skip TDD | See |
| 问题 | 解决方案 |
|---|---|
| 不知道如何编写测试 | 写出你期望存在的API。先写断言。询问用户。 |
| 测试过于复杂 | 设计过于耦合。简化接口。 |
| 必须Mock所有对象 | 代码过于耦合。使用依赖注入。 |
| 测试立即通过 | 强化断言。确认它测试的是新行为。 |
| 首次运行出现导入错误 | 先创建桩文件/函数,再重新运行。 |
| 想要跳过TDD | 参考 |
references/test-quality.mdreferences/test-case-derivation.mdreferences/mocking-guidelines.mdreferences/design-and-refactoring.mdreferences/discipline.mdreferences/legacy-mode.mdscripts/detect_test_env.shreferences/test-quality.mdreferences/test-case-derivation.mdreferences/mocking-guidelines.mdreferences/design-and-refactoring.mdreferences/discipline.mdreferences/legacy-mode.mdscripts/detect_test_env.sh