unit-testing-4.3-快速反馈与可维护性

2026-05-09 ⏳0.5分钟(0.2千字)

4.3 The third and fourth pillars: Fast feedback and maintainability

4.3 第三和第四大支柱:快速反馈与可维护性

仅个人学习使用,支持正版。

书名:Unit Testing: Principles, Practices, and Patterns

In this section, I talk about the two remaining pillars of a good unit test:

本节会讨论优秀单元测试剩下的两个支柱:

As you may remember from chapter 2, fast feedback is an essential property of a unit test. The faster the tests, the more of them you can have in the suite and the more often you can run them.

你可能还记得第 2 章提到过,快速反馈是单元测试的一个基本属性。测试越快,测试套件中能够包含的测试就越多,你也能越频繁地运行它们。

With tests that run quickly, you can drastically shorten the feedback loop, to the point where the tests begin to warn you about bugs as soon as you break the code, thus reducing the cost of fixing those bugs almost to zero. On the other hand, slow tests delay the feedback and potentially prolong the period during which the bugs remain unnoticed, thus increasing the cost of fixing them. That’s because slow tests discourage you from running them often, and therefore lead to wasting more time moving in a wrong direction.

有了运行快速的测试,你可以大幅缩短反馈循环,甚至让测试在你刚刚破坏代码时就提醒你存在缺陷,从而几乎把修复这些缺陷的成本降为零。另一方面,慢速测试会延迟反馈,并可能延长缺陷未被发现的时间,从而增加修复成本。这是因为慢速测试会降低你频繁运行它们的意愿,进而让你在错误方向上浪费更多时间。

Finally, the fourth pillar of good units tests, the maintainability metric, evaluates maintenance costs. This metric consists of two major components:

最后,优秀单元测试的第四大支柱,也就是可维护性指标,用来评估维护成本。这个指标由两个主要部分组成: