时差

工程开发 社区

技能解读

tdd 是「测试驱动开发」的紧凑版纪律:测试必须通过 public interface 验证 behavior,绝不耦合实现细节。代码可以整段重写,测试不该跟着崩——这是 good test 与 bad test 的核心分水岭。

设计思路

作者把 good test 称为 integration-style:跑真实代码路径走 public API,描述「系统能干什么」("user can checkout with valid cart"),refactor 不会让它崩。bad test 则被钉在实现上:mock 内部协作者、测私方法、绕开接口直接查 DB——它们在你 rename 一个内部函数时就崩,但 behavior 根本没变。

反模式:横切(Horizontal Slices)

作者明令禁止「先写完所有测试再写所有实现」——这是把 RED 当「写完所有测试」、把 GREEN 当「写完所有代码」。其结果是:测试在批量阶段测的是想象中的 behavior,不是真实 behavior;测试沦为对「数据结构 / 函数签名形状」的描摹,对真实变化不敏感(behavior 坏了它还过 / behavior 没坏它却挂);你冲在自己的 headlights 前面。

正确做法:垂直 tracer bullets

RED → GREEN: test1 → impl1,RED → GREEN: test2 → impl2……一条测试驱出一条实现,下一条测试响应你刚学到的东西,因为代码刚写完,你最清楚什么 behavior 重要、怎么验证。

工作流

1. Planning:探代码时用项目领域词典,让测试名 + 接口词汇与项目语言一致;尊重要触及区域的 ADR;动手前与用户确认 interface 变化。后续每个 RED→GREEN→REFACTOR 循环都遵照配套文档:tests.md(例子)、mocking.md(mock 的边界)。

适合谁

  • 想把测试变成可信赖的回归网而不是 ceremony
  • refactor 频繁的代码库,希望测试不在改名时崩
  • 团队里有人写「测了 100 个签名」式 test,要校正方向

不适合

  • 一次性脚本 / 抛弃式 prototype
  • 生成代码 / 配置文件——这些走别的验证手段更经济

配套

test-driven-development(同名更长版含 Red-Green-Refactor 详细图与例子)、verification-before-completion(落地前自检)、subagent-driven-development(每条 task 内部走 RED→GREEN)、receiving-code-review(review 反馈时不要乱删测试)。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@mattpocock · 未声明许可证
上下文用量评级
中等消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# Test-Driven Development

TDD is the red → green loop. This skill is the reference that makes that loop produce tests worth keeping: what a good test is, where tests go, the anti-patterns, and the rules of the loop. Every section applies on every cycle: consult them before and during the loop, not after.

When exploring the codebase, read `CONTEXT.md` (if it exists) so test names and interface vocabulary match the project's domain language, and respect ADRs in the area you're touching.

## What a good test is

Tests verify behavior through public interfaces, not implementation details. Code can change entirely; tests shouldn't. A good test reads like a specification: "user can checkout with valid cart" tells you exactly what capability exists, and it survives refactors because it doesn't care about internal structure.

See [tests.md](tests.md) for examples and [mocking.md](mocking.md) for mocking guidelines.

## Seams: where tests go

A **seam** is the public boundary you test at: the interface where you observe behavior without reaching inside. Tests live at seams, never against internals.

**Test only at pre-agreed seams.** Before writing any test, write down the seams under test and confirm them with the user. No test is written at an unconfirmed seam. You can't test everything, so agreeing the seams up front is how testing effort lands on the critical paths and complex logic instead of every edge case.

Ask: "What's the public interface, and which seams should we test?"

When the shape of that interface is itself in question (how deep the module is, where the seam belongs, what the interface should expose), call the Skill tool with "codebase-design" for the vocabulary. It is the shared source of the module, interface, depth, seam, adapter, leverage and locality terms, and it is a reference to consult, not a session to run.

## Anti-patterns

- **Implementation-coupled**: mocks internal collaborators, tests private methods, or verifies through a side channel (querying the database instead of using the interface). The tell: the test breaks when you refactor but behavior hasn't changed.
- **Tautological**: the assertion recomputes the expected value the way the code does (`expect(add(a, b)).toBe(a + b)`, a snapshot derived by hand the same way, a constant asserted equal to itself), so it passes by construction and can never disagree with the code. Expected values must come from an independent source of truth: a known-good literal, a worked example, the spec.
- **Horizontal slicing**: writing all tests first, then all implementation. Bulk tests verify _imagined_ behavior: you test the _shape_ of things rather than user-facing behavior, the tests go insensitive to real changes, and you commit to test structure before understanding the implementation. Work in **vertical slices** instead: one test → one implementation → repeat, each test a **tracer bullet** that responds to what the last cycle taught you.

## Rules of the loop

- **Red before green.** Write the failing test first, then only enough code to pass it. Don't anticipate future tests or add speculative features.
- **One slice at a time.** One seam, one test, one minimal implementation per cycle.
- **Refactoring is not part of the loop.** It belongs to the review stage (see the `code-review` skill), not the red → green implementation cycle.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。