Make Each Characterization Test Fail Once Before You Refactor
说明与用途
永不失败的测试无法防止隐藏的行为更改。通过注入临时“接缝”、捕获行为并确保每个测试在有意修改时失败,您可以安全地重构代码。
问题
Refactoring messy code often leaves tests that never fail, masking silent behavior changes.
结果
Tests now assert on observable behavior and side‑effects, ensuring refactors cannot silently alter functionality.
工作流
- Inject temporary test harness seams to control randomness, time, network, and global writers.
- Capture the function’s behavior (including side‑effects) into a snapshot and write goldens to disk.
- Commit the goldens so they can be diffed and reviewed.
- For each case, deliberately mutate the code under test and confirm the test fails.
- Only after a failure is observed, make the smallest safe change and re‑run the test.
- Repeat until all cases survive one intentional mutation.
证据
A test only earns trust when you can make it fail on purpose.
Write goldens to disk. Commit them. A reviewable diff beats a magic assertion.
- 记录日期: 2026-09-20T05:45:17.593Z