test-first-implementation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest-First Implementation
测试优先实现
Production code follows a failing test.
First inspect the entry point, callers, nearest convention, and coverage.
Use for unknown causes.
systematic-debugging先有失败的测试,再修改生产代码。
首先检查入口点、调用方、最相近的既有约定以及测试覆盖情况。
原因未知时,使用 。
systematic-debuggingRed, green, refactor
Red、Green、重构
Test real behavior at a stable observable boundary, not through mock assertions
or a side-channel query. Take expected values from the specification or a worked
example, never from a recomputation of the code. Confirm the test fails for the
missing behavior before writing production code; when you force red by mutating
code, diff against the pristine file to prove the mutation landed. Implement
only what satisfies that test without speculative options, abstractions, or
adjacent cleanup, run the focused test to confirm green, and refactor while
green before moving to the next behavior.
Understand dependencies before mocking. Do not add production APIs only for
tests. Run the broader suite at task or milestone completion, before claiming success.
在稳定且可观察的边界测试真实行为,而不是通过 mock 断言或侧信道查询进行测试。预期值应来自规范或经过完整推演的示例,绝不能通过重新计算代码得出。在编写生产代码前,确认测试会因目标行为缺失而失败;如果通过变异代码强制进入 red 状态,应与未修改的原始文件进行 diff,以证明变异已准确生效。只实现该测试所要求的内容,不添加推测性的选项、抽象或顺带清理;运行聚焦测试以确认进入 green 状态,然后在保持 green 的同时进行重构,再处理下一个行为。
在 mock 之前先理解依赖关系。不要仅为测试而添加生产 API。任务或里程碑完成时、宣称成功之前,运行更广泛的测试套件。
Code judgment
代码判断
Before writing custom code, check existing code, the standard library, native
platform features, and installed dependencies. Require behavioral fit; justify
new dependencies by missing capability or maintenance benefit.
When simplifying changed code, preserve public contracts, trust-boundary
validation, data-loss error handling, security, accessibility, and required
tests. Do not sacrifice readability or correctness for line count. One caller
or implementation does not make a boundary unnecessary.
For a deliberate simplification with a known limit, comment its ceiling and
revisit condition using local conventions. Ordinary code needs no debt marker.
Validate untrusted data once at entry and make error ownership explicit:
expected, retryable, or terminal.
Load exactly one language reference when local code cannot settle a
language-specific engineering decision:
- Go: references/go.md
- Python: references/python.md
- TypeScript: references/typescript.md
For generated output, prototypes destined for deletion, or prose-only
configuration without executable behavior, use the nearest deterministic check.
A reversible, low-impact edit such as copy, styling, or a config value needs
the existing checks, not a new test; a test that restates the implementation
proves nothing. Never exempt product behavior. If a direct executable oracle beats a brittle
characterization test, record the exception. Confirm the pre-change deficiency
and run that oracle before and after the change.
编写自定义代码前,检查现有代码、标准库、原生平台功能以及已安装的依赖。新增依赖必须符合行为需求,并应以能力缺失或维护收益作为引入依据。
简化已修改的代码时,应保留公共契约、信任边界验证、防止数据丢失的错误处理、安全性、可访问性以及必要测试。不得为减少代码行数而牺牲可读性或正确性。单个调用方或单个实现并不意味着边界不再必要。
如果有意进行简化且已知其上限,请通过注释说明该上限,并依据本地约定重新审视适用条件。普通代码无需添加技术债务标记。
在入口处统一验证不可信数据,并明确错误处理责任,将错误区分为:预期错误、可重试错误或终止性错误。
当本地代码无法确定与语言相关的工程决策时,只加载一份语言参考:
- Go:references/go.md
- Python:references/python.md
- TypeScript:references/typescript.md
对于生成输出、注定删除的原型,或不含可执行行为的纯文本配置,使用最贴近的确定性检查。文案、样式或配置值等可逆的低影响修改应使用现有检查,而不是新增测试;仅仅复述实现逻辑的测试无法证明任何问题。绝不能豁免产品行为。如果直接可执行的 oracle 优于脆弱的 characterization test,请记录这一例外情况。确认变更前存在的缺陷,并在变更前后运行该 oracle。