wip-7.0

2026-06-05 ⏳0.9分钟(0.4千字)

Chapter 7 Refactoring toward valuable unit tests

第 7 章 重构为有价值的单元测试

This chapter covers

本章涵盖:

In chapter 1, I defined the properties of a good unit test suite:

第 1 章中,我定义了优秀单元测试套件的属性:

Chapter 4 covered the topic of recognizing a valuable test using the four attributes: protection against regressions, resistance to refactoring, fast feedback, and maintainability. And chapter 5 expanded on the most important one of the four: resistance to refactoring.

第 4 章使用四个属性讨论了如何识别有价值的测试:防止回归、抗重构能力、快速反馈和可维护性。第 5 章则扩展了其中最重要的一个属性:抗重构能力。

As I mentioned earlier, it’s not enough to recognize valuable tests, you should also be able to write such tests. The latter skill requires the former, but it also requires that you know code design techniques. Unit tests and the underlying code are highly intertwined, and it’s impossible to create valuable tests without putting effort into the code base they cover.

正如我前面提到的,仅仅识别有价值的测试还不够,你还应该能够编写这样的测试。后一项能力需要前一项能力,但也要求你懂得代码设计技术。单元测试与其底层代码高度交织,如果不在它们覆盖的代码库上投入精力,就不可能创建有价值的测试。

You saw an example of a code base transformation in chapter 6, where we refactored an audit system toward a functional architecture and, as a result, were able to apply output-based testing. This chapter generalizes this approach onto a wider spectrum of applications, including those that can’t use a functional architecture. You’ll see practical guidelines on how to write valuable tests in almost any software project.

第 6 章中你看到过一个代码库转换示例:我们把审计系统重构为函数式架构,因此能够应用基于输出的测试。本章会把这种方法推广到更广泛的应用中,包括那些无法使用函数式架构的应用。你会看到如何在几乎任何软件项目中编写有价值测试的实用指南。