Test for Required Behavior, not Incidental Behavior
来源:互联网 发布:电信卡怎么设置4g网络 编辑:程序博客网 时间:2024/05/29 17:39
A common pitfall in testing is to assume that exactly what an implementation does is precisely what you want to test for. At first glance this sounds more like a virtue than a pitfall. Phrased another way, however, the issue becomes more obvious: A common pitfall in testing is to hardwire tests to the specifics of an implementation, where those specifics are incidental and have no bearing on the desired functionality.
When tests are hardwired to implementation incidentals, changes to the implementation that are actually compatible with the required behavior may cause tests to fail, leading to false positives. Programmers typically respond either by rewriting the test or by rewriting the code. Assuming that a false positive is actually a true positive is often a consequence of fear, uncertainty, or doubt. It has the effect of raising the status of incidental behavior to required behavior. In rewriting a test, programmers either refocus the test on the required behavior (good) or simply hardwire it to the new implementation (not good). Tests need to be sufficiently precise, but they also need to be accurate.
For example, in a three-way comparison, such as C's strcmp
or Java'sString.compareTo
, the requirements on the result are that it is negative if the left-hand side is less than the right, positive if the left-hand side is greater than the right, and zero if they are considered equal. This style of comparison is used in many APIs, including the comparator for C'sqsort
function andcompareTo
in Java's Comparable
interface. Although the specific values-1
and+1
are commonly used in implementations to signifyless than andgreater than, respectively, programmers often mistakenly assume that these values represent the actual requirement and consequently write tests that nail this assumption up in public.
A similar issue arises with tests that assert spacing, precise wording, and other aspects of textual formatting and presentation that are incidental. Unless you are writing, for example, an XML generator that offers configurable formatting, spacing should not be significant to the outcome. Likewise, hardwiring placement of buttons and labels on UI controls reduces the option to change and refine these incidentals in future. Minor changes in implementation and inconsequential changes in formatting suddenly become build breakers.
Overspecified tests are often a problem with whitebox approaches to unit testing. Whitebox tests use the structure of the code to determine the test cases needed. The typical failure mode of whitebox testing is that the tests end up asserting that the code does what the code does. Simply restating what is already obvious from the code adds no value and leads to a false sense of progress and security.
To be effective, tests need to state contractual obligations rather than parrot implementations. They need to take a blackbox view of the units under test, sketching out the interface contracts in executable form. Therefore, align tested behavior with required behavior.
By Kevlin Henney
This work is licensed under a Creative Commons Attribution 3
- Test for Required Behavior, not Incidental Behavior
- Test for Required Behavior, Not Incidental Behavior
- behavior
- BEHAVIOR
- Behavior
- Behavior
- Encapsulate Behavior, not Just State
- Encapsulate Behavior, Not Just State
- [Java] Behavior Driven Test supported by Mockito
- air jordan pas cher not only did not eat hot dogs behavior for extra points
- Material Status Behavior for Org Transfer
- Custom Editing Behavior for DataGridView TextBox Columns
- Position Bias Correction for Search Behavior Analysis
- CSS Behavior
- CSS Behavior
- Behavior Tree
- Behavior Designer
- CoordinatorLayout behavior
- Step Back and Automate, Automate, Automate
- java HttpClient Post实例
- JNI学习(5)(我是菜鸟)
- Take Advantage of Code Analysis Tools
- 俞敏洪一分钟励志演讲:
- Test for Required Behavior, not Incidental Behavior
- Android的permission大全
- 在交互细节上,Android 与 iOS 有哪些区别?
- PCB布板一些简易常用规则
- JNI学习(6)(我是菜鸟)
- const和指针
- Discuz!X 插件制作教程( 网站基本数据)
- 黑马程序员_第一天总结
- 学习论坛