unit-testing-4-总结
zeroSummary
总结
仅个人学习使用,支持正版。
书名:Unit Testing: Principles, Practices, and Patterns
- A good unit test has four foundational attributes that you can use to analyze any automated test, whether unit, integration, or end-to-end: protection against regressions, resistance to refactoring, fast feedback, and maintainability.
一个好的单元测试具有四个基础属性,你可以用它们分析任何自动化测试,无论是单元测试、集成测试还是端到端测试:防止回归、抵抗重构、快速反馈和可维护性。 - Protection against regressions is a measure of how good the test is at indicating the presence of bugs (regressions). The more code the test executes (both your code and the code of libraries and frameworks used in the project), the higher the chance this test will reveal a bug.
防止回归衡量的是测试在指出缺陷(回归)存在方面表现如何。测试执行的代码越多(既包括你的代码,也包括项目中使用的库和框架代码),这个测试发现缺陷的概率就越高。 - Resistance to refactoring is the degree to which a test can sustain application code refactoring without producing a false positive.
抵抗重构指的是,在不产生假阳性的情况下,测试能够承受应用代码重构的程度。 - A false positive is a false alarm—a result indicating that the test fails, whereas the functionality it covers works as intended. False positives can have a devastating effect on the test suite.
假阳性是误报,也就是测试结果显示失败,但它覆盖的功能实际上按预期工作。假阳性可能对测试套件产生毁灭性影响。- They dilute your ability and willingness to react to problems in code, because you get accustomed to false alarms and stop paying attention to them.
它们会削弱你对代码问题做出反应的能力和意愿,因为你会习惯误报,并停止关注它们。 - They diminish your perception of tests as a reliable safety net and lead to losing trust in the test suite.
它们会削弱你把测试视为可靠安全网的认知,并导致你失去对测试套件的信任。
- They dilute your ability and willingness to react to problems in code, because you get accustomed to false alarms and stop paying attention to them.
- False positives are a result of tight coupling between tests and the internal implementation details of the system under test. To avoid such coupling, the test must verify the end result the SUT produces, not the steps it took to do that.
假阳性是测试与被测系统内部实现细节紧密耦合的结果。要避免这种耦合,测试必须验证 SUT 产生的最终结果,而不是它为了产生结果所采取的步骤。 - Protection against regressions and resistance to refactoring contribute to test accuracy. A test is accurate insofar as it generates a strong signal (is capable of finding bugs, the sphere of protection against regressions) with as little noise (false positives) as possible (the sphere of resistance to refactoring).
防止回归和抵抗重构共同提升测试准确性。当一个测试能够产生强信号(能够发现缺陷,这是防止回归的范畴),同时尽可能少地产生噪声(假阳性,这是抵抗重构的范畴)时,它就是准确的。 - False positives don’t have as much of a negative effect in the beginning of the project, but they become increasingly important as the project grows: as important as false negatives (unnoticed bugs).
在项目早期,假阳性的负面影响没有那么大;但随着项目增长,它们会变得越来越重要,最终和假阴性(未被发现的缺陷)一样重要。 - Fast feedback is a measure of how quickly the test executes.
快速反馈衡量的是测试执行得有多快。 - Maintainability consists of two components: how hard it is to understand the test, and how hard it is to run the test.
可维护性由两个部分组成:理解测试有多难,以及运行测试有多难。- How hard it is to understand the test. The smaller the test, the more readable it is.
理解测试有多难。测试越小,就越容易阅读。 - How hard it is to run the test. The fewer out-of-process dependencies the test reaches out to, the easier it is to keep them operational.
运行测试有多难。测试接触的进程外依赖越少,保持这些依赖可用就越容易。
- How hard it is to understand the test. The smaller the test, the more readable it is.
- A test’s value estimate is the product of scores the test gets in each of the four attributes. If the test gets zero in one of the attributes, its value turns to zero as well.
一个测试的价值估算,是它在四个属性上得分的乘积。如果测试在其中某个属性上得分为零,它的价值也会变成零。 - It’s impossible to create a test that gets the maximum score in all four attributes, because the first three—protection against regressions, resistance to refactoring, and fast feedback—are mutually exclusive. The test can only maximize two out of the three.
创建一个在四个属性上都得最高分的测试是不可能的,因为前三个属性——防止回归、抵抗重构和快速反馈——相互排斥。测试最多只能在这三个属性中最大化两个。 - Resistance to refactoring is non-negotiable because whether a test possesses this attribute is mostly a binary choice: the test either has resistance to refactoring or it doesn’t. The trade-off between the attributes comes down to the choice between protection against regressions and fast feedback.
抵抗重构是不可谈判的,因为一个测试是否具备这个属性基本上是二元选择:要么具备抵抗重构能力,要么不具备。属性之间的取舍最终落在防止回归和快速反馈之间。 - The Test Pyramid advocates for a certain ratio of unit, integration, and end-to-end tests: end-to-end tests should be in the minority, unit tests in the majority, and integration tests somewhere in the middle.
测试金字塔主张单元测试、集成测试和端到端测试之间保持某种比例:端到端测试应占少数,单元测试应占多数,集成测试位于中间。 - Different types of tests in the pyramid make different choices between fast feedback and protection against regressions. End-to-end tests favor protection against regressions, while unit tests favor fast feedback.
金字塔中的不同测试类型,会在快速反馈和防止回归之间做出不同选择。端到端测试偏向防止回归,而单元测试偏向快速反馈。 - Use the black-box testing method when writing tests. Use the white-box method when analyzing the tests.
写测试时使用黑盒测试方法;分析测试时使用白盒方法。