wip-2.0

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

Chapter 2 What is a unit test?

第 2 章 什么是单元测试?

This chapter covers:

本章内容包括:

As mentioned in chapter 1, there are a surprising number of nuances in the definition of a unit test. Those nuances are more important than you might think—so much so that the differences in interpreting them have led to two distinct views on how to approach unit testing.

正如第 1 章提到的,单元测试的定义中有数量惊人的细微差别。这些细微差别比你想象的更重要——重要到对它们的不同解释,已经形成了两种截然不同的单元测试实践观点。

These views are known as the classical and the London schools of unit testing. The classical school is called “classical” because it’s how everyone originally approached unit testing and test-driven development. The London school takes root in the programming community in London. The discussion in this chapter about the differences between the classical and London styles lays the foundation for chapter 5, where I cover the topic of mocks and test fragility in detail.

这两种观点被称为单元测试的经典学派和伦敦学派。经典学派之所以叫“经典”,是因为它是人们最初实践单元测试和测试驱动开发的方式。伦敦学派则源于伦敦的程序员社区。本章关于经典风格和伦敦风格差异的讨论,会为第 5 章打下基础;第 5 章会详细讨论 mock 和测试脆弱性。

Let’s start by defining a unit test, with all due caveats and subtleties. This definition is the key to the difference between the classical and London schools.

我们先从定义单元测试开始,并保留所有必要的限定和细微之处。这个定义是理解经典学派与伦敦学派差异的关键。