Test for Required Behavior, Not Incidental Behavior

翻译 2015年07月07日 15:07:33

Test for Required Behavior, Not Incidental Behavior

Kevlin Henney
A COMMON PITFALL IN TESTING is to assume that exactly what an imple- mentation 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 imple- mentation 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 behav- ior (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 Java’s String.compareTo or C’s strcmp, the requirements on the result are that it is negative if the lefthand side is less than the right, positive if the lefthand 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’s qsort function and compareTo in Java’s Comparable interface. Although the specific values –1 and +1 are commonly used
160 97 Things Every Programmer Should Know

in implementations to signify less than and greater than, respectively, program- mers 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 format- ting, 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 the 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 test- ing. 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 par- rot 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.

Test for Required Behavior, not Incidental Behavior

A common pitfall in testing is to assume that exactly what an implementation does is precisely what ...
  • hellominix
  • hellominix
  • 2013年04月04日 09:17
  • 209


  • u011467537
  • u011467537
  • 2016年12月09日 17:07
  • 675


  • litefish
  • litefish
  • 2016年08月25日 14:30
  • 1123

Behavior Designer 中文版教程

Behavior Designer 概述 Behavior Designer 是一个行为树插件!是为了让设计师,程序员,美术人员方便使用的可视化编辑器!Behavior Designer 提供了强大的...
  • mango9126
  • mango9126
  • 2017年05月11日 19:10
  • 674

Behavior Designer 入门笔记.

1.导入--拖进来即可.. 2.打开编辑界面 .Tools--> Behavior Designer -->edit; 3.选中游戏物体,给其添加行为树. 4.s...
  • u012930316
  • u012930316
  • 2017年05月11日 12:11
  • 236

[Unity插件]Behavior Designer:节点

Selector:类似于"or",从左到右执行节点,当遇到子节点返回失败时,继续执行;当遇到子节点返回成功时,返回成功,不再继续执行。 Sequence:类似于"and",从左到右执行节点,当遇到子节...
  • lyh916
  • lyh916
  • 2015年05月03日 14:02
  • 1744

Material Design系列,自定义Behavior实现Android知乎首页

Material Design系列,自定义自定义Behavior实现Android知乎首页,用Behavior实现知乎首页上滑或者下滑时FAB和TAB导航的显示和隐藏,留给用户更多的位置来显示内容。...
  • yanzhenjie1003
  • yanzhenjie1003
  • 2016年07月18日 23:53
  • 25713

Android Behavior

原文出处:http://www.jianshu.com/p/a506ee4afecb Behavior是Android新出的Design库里新增的布局概念。Behavior只有是Co...
  • qq_15807167
  • qq_15807167
  • 2017年01月18日 14:50
  • 1621


CSS里面的behavior属性的作用是什么,怎么使用 2011-08-30 15:39  在进行CSS网页布局的时候,我们经遇到刷新要保留表单里内容的时候,习惯的做法使用cookie,...
  • A_post
  • A_post
  • 2011年09月09日 23:59
  • 1145

Behavior Designer 干货总结

Unity3D上,实现简单AI最好的工具,莫过于BehaviorDesigner了。 虽然我自己也写过行为树,虽然也用过别的小伙伴写的行为树,虽然也用过企鹅社公开的行为树,虽然一大堆,但是的确没有哪个...
  • WangHaoDiablo
  • WangHaoDiablo
  • 2016年09月19日 16:59
  • 2033
您举报文章:Test for Required Behavior, Not Incidental Behavior