wip-1.1

2026-06-05 ⏳1.8分钟(0.7千字)

1.1 The current state of unit testing

1.1 单元测试的现状

For the past two decades, there’s been a push toward adopting unit testing. The push has been so successful that unit testing is now considered mandatory in most companies. Most programmers practice unit testing and understand its importance. There’s no longer any dispute as to whether you should do it. Unless you’re working on a throwaway project, the answer is, yes, you do.

过去二十年里,业界一直在推动采用单元测试。这股推动非常成功,以至于现在大多数公司都把单元测试视为必需品。大多数程序员都会实践单元测试,也理解它的重要性。如今已经不再争论“是否应该做单元测试”。除非你正在做一个用完即丢的项目,否则答案就是:是的,你应该做。

When it comes to enterprise application development, almost every project includes at least some unit tests. A significant percentage of such projects go far beyond that: they achieve good code coverage with lots and lots of unit and integration tests. The ratio between the production code and the test code could be anywhere between 1:1 and 1:3 (for each line of production code, there are one to three lines of test code). Sometimes, this ratio goes much higher than that, to a whopping 1:10.

在企业应用开发中,几乎每个项目至少都会包含一些单元测试。相当一部分项目还远不止如此:它们通过大量单元测试和集成测试达到了不错的代码覆盖率。生产代码和测试代码之间的比例可能在 1:1 到 1:3 之间,也就是说每一行生产代码对应一到三行测试代码。有时这个比例甚至会高得多,达到惊人的 1:10。

But as with all new technologies, unit testing continues to evolve. The discussion has shifted from “Should we write unit tests?” to “What does it mean to write good unit tests?” This is where the main confusion still lies.

但和所有新技术一样,单元测试仍在不断演进。讨论的焦点已经从“我们该不该写单元测试?”转向“写出好的单元测试到底意味着什么?”主要困惑仍然存在于这里。

You can see the results of this confusion in software projects. Many projects have automated tests; they may even have a lot of them. But the existence of those tests often doesn’t provide the results the developers hope for. It can still take programmers a lot of effort to make progress in such projects. New features take forever to implement, new bugs constantly appear in the already implemented and accepted functionality, and the unit tests that are supposed to help don’t seem to mitigate this situation at all. They can even make it worse.

你可以在软件项目中看到这种困惑带来的结果。许多项目都有自动化测试,甚至有很多自动化测试。但这些测试的存在,往往并没有带来开发者期望的结果。在这样的项目中,程序员仍然可能需要付出大量努力才能推进一点点进展。新功能实现起来遥遥无期,已经实现并验收的功能里不断出现新缺陷,而本应提供帮助的单元测试似乎完全没有缓解这种情况,甚至可能让情况变得更糟。

It’s a horrible situation for anyone to be in—and it’s the result of having unit tests that don’t do their job properly. The difference between good and bad tests is not merely a matter of taste or personal preference, it’s a matter of succeeding or failing at this critical project you’re working on.

这对任何人来说都是糟糕的处境,而它正是由那些没有正确发挥作用的单元测试造成的。好测试和坏测试之间的差异,不只是品味或个人偏好的问题;它关乎你正在参与的关键项目最终是成功还是失败。

It’s hard to overestimate the importance of the discussion of what makes a good unit test. Still, this discussion isn’t occurring much in the software development industry today. You’ll find a few articles and conference talks online, but I’ve yet to see any comprehensive material on this topic.

“什么造就一个好的单元测试”这个话题的重要性,再怎么强调都不为过。然而,今天的软件开发行业中,这样的讨论并不多。你可以在网上找到一些文章和会议演讲,但我还没有看到关于这个主题的系统性材料。

The situation in books isn’t any better; most of them focus on the basics of unit testing but don’t go much beyond that. Don’t get me wrong. There’s a lot of value in such books, especially when you are just starting out with unit testing. However, the learning doesn’t end with the basics. There’s a next level: not just writing tests, but doing unit testing in a way that provides you with the best return on your efforts. When you reach this point, most books pretty much leave you to your own devices to figure out how to get to that next level.

书籍方面的情况也没有好到哪里去;大多数书都聚焦于单元测试基础,但没有进一步深入。别误会,这类书很有价值,尤其是在你刚开始学习单元测试时。然而,学习并不会在基础知识处停止。还有下一层:不只是写测试,而是以一种能让你的努力获得最佳回报的方式进行单元测试。当你到达这个阶段时,大多数书基本上就把你留给自己,让你自己摸索如何进入下一层。

This book takes you there. It teaches a precise, scientific definition of the ideal unit test. You’ll see how this definition can be applied to practical, real-world examples. My hope is that this book will help you understand why your particular project may have gone sideways despite having a good number of tests, and how to correct its course for the better.

本书会带你到达那里。它会教授理想单元测试的一个精确、科学的定义。你会看到这个定义如何应用到真实世界的实践例子中。我希望本书能帮助你理解:为什么你的项目明明有相当数量的测试,却仍然可能走偏;以及如何把它调整到更好的方向上。

You’ll get the most value out of this book if you work in enterprise application development, but the core ideas are applicable to any software project.

如果你从事企业应用开发,你会从本书中获得最大价值。不过,本书的核心思想适用于任何软件项目。

What is an enterprise application?

什么是企业应用?

An enterprise application is an application that aims at automating or assisting an organization’s inner processes. It can take many forms, but usually the characteristics of an enterprise software are:

企业应用是指用于自动化或辅助组织内部流程的应用。它可以有许多形式,但企业软件通常具有以下特征: