360 Secure SkillHub
360 安全技能中心

principle-test-behavior-not-implementation

该 Skill 提供测试编写与评审原则:用“破坏行为时测试应失败、只改内部实现时测试应通过”两项检查,判断测试是否真正验证可观察行为;建议通过公共接口并使用需求推导的期望值,断言调用方关心的错误类型。

分类:开发
backnotprop
热度 66.41
最近更新 2026-11-24 05:23:57

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add backnotprop/pstack/principle-test-behavior-not-implementation
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
principle-test-behavior-not-implementation
description
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.
disable-model-invocation
true

Test Behavior, Not Implementation

A test is a claim about what users of the code can observe through its interface: return values, errors, state they can read back, and effects on outside systems. Private helpers, internal call order, and intermediate data are implementation.

Judge each test against two imagined edits:

  • Break the behavior (a wrong value, a skipped or doubled effect). If the test still passes, it proves nothing. The usual causes are a loose assertion, an expected value computed by the code under test, and an assertion on data the test built itself.
  • Change the internals and keep every observable result. If the test fails, it is pinned to the implementation.

Keep a test that catches the first edit and survives the second. Rewrite or delete the rest. Drive the code through its public interface with a concrete input, and derive the expected value from the requirement. What an external service receives is behavior. A call to an internal helper is not. Pair "this never happens" with a case where it does happen. Assert the error type that callers branch on, not the message.