tdd
测试驱动开发
Section titled “测试驱动开发”TDD 是 red → green 循环。这个 skill 是这套循环的参考指南,帮助你写出值得保留的测试:什么样的测试才好、测试该放在哪里、有哪些反模式,以及循环有哪些规则。每一节在每个循环里都适用,要在循环前和循环中查看,不是等结束后再看。
探索代码库时,如果有 GLOSSARY.md,就先读它,让测试名称和接口词汇符合项目的领域语言。也要遵守你正在改动区域的 ADR。
什么样的测试才好
Section titled “什么样的测试才好”测试要通过公开接口验证行为,而不是实现细节。代码可以整个换掉,测试不该跟着变。好的测试读起来像一份规格:「用户可以用有效购物车完成结账」能清楚说明有什么能力,也能经受重构,因为它不关心内部结构。
示例见 tests.md,mocking 指南见 mocking.md。
接缝:测试该放在哪里
Section titled “接缝:测试该放在哪里”接缝是你用来测试的公开边界:在这里观察行为,不需要深入内部。
只在事先谈妥的接缝上测试。 写任何测试前,先记下要测的接缝,并和用户确认。没有确认过的接缝上不能写测试。你不可能什么都测,所以先谈好接缝,才能把测试精力放在关键路径和复杂逻辑上,而不是每个边缘情况。
可以问:「公开接口是什么?我们该测哪些接缝?」给每个提议的接缝写一句说明,说明它能发现什么、发现不了什么。
如果接口形状本身还没定,比如模块应该多深、接缝该放哪里、接口要暴露什么,就通过 Skill 工具调用 codebase-design 获取共同词汇。它是模块、接口、深度、接缝、适配器、杠杆和局部性的共同参考。查阅它就好,不要把它当作一个会话来运行。
- 和实现绑得太紧:mock 内部协作者、测试私有方法,或绕过接口从旁边验证,比如直接查数据库。判断方法是:重构后行为没变,测试却坏了。
- 同义反复:断言用和代码相同的方式重新计算预期值,比如
expect(add(a, b)).toBe(a + b)、手工按同样方式生成快照,或断言一个常量等于自己。这样测试注定会通过,永远无法和代码意见不一致。预期值必须来自独立来源,例如已知正确的字面值、算过的例子或规格。 - 横向切片:先一次性写完所有测试,再写所有实现。批量测试验证的是想象出来的行为:你测的是功能的形状,而不是用户能做什么;测试对真实变化不敏感,而且在理解实现之前就定下了测试结构。改用纵向切片:一个测试、一个实现,再重复。每个测试都是回应上一轮新发现的曳光弹。
- 先红再绿。 先写一个会失败的测试,再只写足以让它通过的代码。不要提前设想未来测试,也不要添加猜测性的功能。
- 一次一个切片。 每轮只做一个接缝、一个测试、一个最小实现。
- 循环里不做重构。 重构留到 review 阶段(见
code-reviewskill),不属于 red → green 实现循环。
原文链接和 commit
Section titled “原文链接和 commit”读者可以点名用它,模型也可以自己调用它。它在 Claude Code 插件里。
tdd 提供测试驱动开发的参考规则,帮助你确认测试接缝、按单个行为推进 red → green 循环,并避开与实现耦合、同义反复和横向切片等反模式。
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.- 仓库 mattpocock/skills
- 文件
skills/engineering/tdd/SKILL.md - 提交
d81f3a1 - 说明页
docs/engineering/tdd.md - 同目录文件
skills/engineering/tdd/agents/openai.yaml - 同目录文件
skills/engineering/tdd/mocking.md - 同目录文件
skills/engineering/tdd/tests.md
英文 SKILL.md
---name: tdddescription: Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.---
# 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 `GLOSSARY.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?" Give each proposed seam a one-line note on what it catches and what it misses.
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.非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。
技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock