npx skills add https://github.com/mattpocock/skills --skill tdd核心原则:测试应通过公共接口验证行为,而非实现细节。代码可以完全改变;测试不应如此。
良好的测试是集成风格的:它们通过公共 API 执行真实的代码路径。它们描述系统“做什么”,而不是“如何做”。一个好的测试读起来像一份规范——“用户可以使用有效的购物车结账”准确地告诉你存在什么功能。这些测试在重构后依然有效,因为它们不关心内部结构。
糟糕的测试与实现紧密耦合。它们模拟内部协作者、测试私有方法,或通过外部手段验证(例如直接查询数据库而不是使用接口)。警告信号:当你重构时测试失败,但行为并未改变。如果你重命名了一个内部函数而测试失败,那么这些测试是在测试实现,而不是行为。
示例请参见 tests.md,模拟指南请参见 mocking.md。
切勿先编写所有测试,然后再编写所有实现。 这是“水平切片”——将 RED 视为“编写所有测试”,将 GREEN 视为“编写所有代码”。
这会产生糟糕的测试:
正确方法:通过追踪弹进行垂直切片。一个测试 → 一个实现 → 重复。每个测试都基于你从前一个周期中学到的东西。因为你刚刚编写了代码,所以确切地知道什么行为重要以及如何验证它。
错误(水平切片):
RED: test1, test2, test3, test4, test5
GREEN: impl1, impl2, impl3, impl4, impl5
正确(垂直切片):
RED→GREEN: test1→impl1
RED→GREEN: test2→impl2
RED→GREEN: test3→impl3
...
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
在编写任何代码之前:
提问:“公共接口应该是什么样子?哪些行为对测试最重要?”
你无法测试所有东西。 与用户确认哪些行为确实最重要。将测试精力集中在关键路径和复杂逻辑上,而不是每个可能的边缘情况。
编写一个测试,确认系统的一个方面:
RED: 为第一个行为编写测试 → 测试失败
GREEN: 编写最少的代码使其通过 → 测试通过
这是你的追踪弹——证明路径端到端是可行的。
对于每个剩余的行为:
RED: 编写下一个测试 → 失败
GREEN: 编写最少的代码使其通过 → 通过
规则:
在所有测试通过后,寻找重构候选:
切勿在 RED 状态下重构。 先达到 GREEN 状态。
[ ] 测试描述的是行为,而非实现
[ ] 测试仅使用公共接口
[ ] 测试能在内部重构后存活
[ ] 代码对于此测试是最简的
[ ] 没有添加推测性的功能
每周安装量
7.6K
代码库
GitHub 星标数
9.5K
首次出现
2026 年 2 月 10 日
安全审计
安装于
opencode7.3K
codex7.3K
github-copilot7.2K
gemini-cli7.2K
amp7.1K
kimi-cli7.1K
Core principle : Tests should verify behavior through public interfaces, not implementation details. Code can change entirely; tests shouldn't.
Good tests are integration-style: they exercise real code paths through public APIs. They describe what the system does, not how it does it. A good test reads like a specification - "user can checkout with valid cart" tells you exactly what capability exists. These tests survive refactors because they don't care about internal structure.
Bad tests are coupled to implementation. They mock internal collaborators, test private methods, or verify through external means (like querying a database directly instead of using the interface). The warning sign: your test breaks when you refactor, but behavior hasn't changed. If you rename an internal function and tests fail, those tests were testing implementation, not behavior.
See tests.md for examples and mocking.md for mocking guidelines.
DO NOT write all tests first, then all implementation. This is "horizontal slicing" - treating RED as "write all tests" and GREEN as "write all code."
This produces crap tests :
Correct approach : Vertical slices via tracer bullets. One test → one implementation → repeat. Each test responds to what you learned from the previous cycle. Because you just wrote the code, you know exactly what behavior matters and how to verify it.
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
...
Before writing any code:
Ask: "What should the public interface look like? Which behaviors are most important to test?"
You can't test everything. Confirm with the user exactly which behaviors matter most. Focus testing effort on critical paths and complex logic, not every possible edge case.
Write ONE test that confirms ONE thing about the system:
RED: Write test for first behavior → test fails
GREEN: Write minimal code to pass → test passes
This is your tracer bullet - proves the path works end-to-end.
For each remaining behavior:
RED: Write next test → fails
GREEN: Minimal code to pass → passes
Rules:
After all tests pass, look for refactor candidates:
Never refactor while RED. Get to GREEN first.
[ ] 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
Weekly Installs
7.6K
Repository
GitHub Stars
9.5K
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode7.3K
codex7.3K
github-copilot7.2K
gemini-cli7.2K
amp7.1K
kimi-cli7.1K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装