principle-test-behavior-not-implementation
它能做什么
为智能体提供一条编写和维护好测试的规则。适合那些测试在每次重构时都会失败,或在代码出错时仍然通过的开发者。
适用场景
- ✓当你编写、修改或保留某个测试的时候。
- ✓当尽管行为没有改变,测试却在每次重构时都失败的时候。
- ✓当你故意破坏某项行为之后,测试仍然通过的时候。
技能链接
https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-test-behavior-not-implementation/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我们每次重构时测试都会失败,即使没有任何面向用户的改动。请把它们改写成检查行为的测试。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会用两种假想的改动来检验每个测试:一种是破坏行为的改动,测试必须能发现;另一种是只改变内部实现的改动,测试必须能通过。它会重写或删除未通过任一检查的测试,并通过代码的公共接口来驱动代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Test Behavior, Not Implementation
A test is a claim about what the code does for its users: a caller, an end user, or another service. Behavior is what those users can observe through the interface they use. That includes return values, errors, state they can read back, and effects on systems outside the code, such as a request sent, a row written, or a message emitted. Implementation is what they cannot observe: private helpers, internal call order, intermediate data, and which internal collaborator did the work.
Judge each test against two imagined edits:
- Break the behavior. Make the code return a wrong value, skip an effect, or do the effect twice. If the test still passes, it proves nothing. The usual causes are an assertion looser than the contract, an expected value computed by the code under test, a restated constant or prompt string that never runs the code that reads it, and an assertion on data that the test built itself.
- Change the internals. Keep every observable result the same and restructure the code underneath. If the test now fails, it is pinned to the implementation. It will need a rewrite on every refactor and still catches no bug.
Keep a test that catches the first edit and survives the second. Rewrite or delete the rest. To write a test that does both, drive the code through its users' interface with a concrete input. Derive the expected value from the requirement, not from a run of the code. Assert what the contract promises, no less and no more.
The contract decides, not the shape of the assertion. When "an empty cart never charges" is the promise, an assertion that the payment client got no call is behavior. Pair it with a case where the charge happens, so that the test shows it can see a call. The payload that a mocked external service receives is behavior, because that service observes it. A call to an internal helper is implementation, because no user can see it. An empty list is the exact answer when no record matches, and a hollow check when the test only needs some result. An exact error message pins wording that no caller reads. The error type that callers branch on is the contract.
A test can pin an invariant instead of a call, such as a key that must exist in two tables or a compile-time type check. The invariant is the behavior.
常见问题
什么算作行为?
代码使用者可以观察到的内容,例如返回值、错误、可读的状态,以及发送消息之类的效果。
什么是薄弱的测试?
其期望值是由被测代码本身计算出来的,或者其断言比约定宽松的测试。
测试应该检查私有辅助函数吗?
不应该。私有辅助函数和内部调用顺序属于实现细节,而不是行为。
详情
作者的原始描述
Apply when you write, change, or keep a test. Assert what the code's users can observe, so that the test fails when that behavior breaks and keeps passing when only the internals change.
repo: cursor/plugins
热度
- GitHub 星标
- 15k
- 排名
- #10990
- 许可证
- —
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
advisor
15k如果你开启该功能,让你的智能体在关键节点咨询一个更强的第二模型,例如在重大决定之前,或在错误上卡住时。
check-agent-compatibility
15k全面检查 AI 编程智能体能否启动、验证和记录某个代码仓库,并返回一个兼容性评分以及主要问题。
cli-for-agents
15k教你的智能体设计或审查命令行工具,使 AI 智能体无需提示即可运行,并提供清晰的帮助文字和示例。
continual-learning
15k通过把回顾过往对话的工作交给专门的记忆更新程序,使你的 AGENTS.md 笔记保持最新。