From mattpocock-skills
Guides test-driven development using red-green-refactor loops with integration-style tests via public interfaces, vertical slicing, tracer bullets, and refactor checklists for features and bug fixes.
npx claudepluginhub vinvcn/mattpocock-skills-zh-cnThis skill uses the workspace's default tool permissions.
**核心原则**:测试应该通过 public interfaces 验证行为,而不是验证 implementation details。代码可以完全改变;测试不该因此改变。
Guides test-driven development with red-green-refactor cycles, vertical slicing, and integration tests via public interfaces. Use for feature building or bug fixes in TDD style.
Guides TDD workflow with red-green-refactor cycle: plan interfaces, tracer bullet tests, minimal implementation to green, refactor under tests. For explicit TDD requests only.
Share bugs, ideas, or general feedback.
核心原则:测试应该通过 public interfaces 验证行为,而不是验证 implementation details。代码可以完全改变;测试不该因此改变。
好测试偏 integration-style:它们通过 public APIs 执行真实 code paths。它们描述系统做_什么_,而不是_怎么做_。好测试读起来像 spec,比如 “user can checkout with valid cart” 清楚说明已有能力。这些测试能经受 refactor,因为它们不关心内部结构。
坏测试和 implementation 绑定。它们 mock 内部 collaborators、测试 private methods,或通过外部手段验证(例如直接查询 database,而不是使用 interface)。警示信号是:你 refactor 了,行为没变,但测试坏了。如果重命名内部函数导致测试失败,那些测试测的是 implementation,不是 behavior。
示例见 tests.md,mocking 指南见 mocking.md。
不要先写所有测试,再写所有实现。 这是 “horizontal slicing”,把 RED 当成“写所有测试”,把 GREEN 当成“写所有代码”。
这会产生糟糕测试:
正确方式:通过 tracer bullets 做 vertical slices。一个 test → 一个 implementation → 重复。每个 test 都响应上一轮学到的东西。因为代码是你刚写的,你知道什么行为重要,以及该如何验证。
WRONG (horizontal):
RED: test1, test2, test3, test4, test5
GREEN: impl1, impl2, impl3, impl4, impl5
RIGHT (vertical):
RED→GREEN: test1→impl1
RED→GREEN: test2→impl2
RED→GREEN: test3→impl3
...
写任何代码之前:
问:“public interface 应该长什么样?哪些 behaviors 最值得测试?”
你无法测试所有东西。 与用户确认最重要的 behaviors。把测试精力放在 critical paths 和复杂逻辑上,而不是每一个边缘情况。
写一个只确认系统一件事的测试:
RED: Write test for first behavior → test fails
GREEN: Write minimal code to pass → test passes
这是你的 tracer bullet,证明路径可以 end-to-end 工作。
对每个剩余 behavior:
RED: Write next test → fails
GREEN: Minimal code to pass → passes
规则:
所有 tests 通过后,寻找 refactor candidates:
RED 状态永远不要 refactor。 先到 GREEN。
[ ] Test describes behavior, not implementation
[ ] Test uses public interface only
[ ] Test would survive internal refactor
[ ] Code is minimal for this test
[ ] No speculative features added