Test Precisely and Concretely

翻译 2015年07月10日 13:52:52

Test Precisely and Concretely

Kevlin Henney
IT IS IMPORTANT TO TEST for the desired, essential behavior of a unit of code, rather than for the incidental behavior of its particular implementation. But this should not be taken or mistaken as an excuse for vague tests. Tests need to be both accurate and precise.
Something of a tried, tested, and testing classic, sorting routines offer an illus- trative example. Implementing a sorting algorithm is not necessarily an every- day task for a programmer, but sorting is such a familiar idea that most people believe they know what to expect from it. This casual familiarity, however, can make it harder to see past certain assumptions.
When programmers are asked, “What would you test for?”, by far and away the most common response is something like, “The result of sorting is a sorted sequence of elements.” While this is true, it is not the whole truth. When prompted for a more precise condition, many programmers add that the result- ing sequence should be the same length as the original. Although correct, this is still not enough. For example, given the following sequence:
31415 9
The following sequence satisfies a postcondition of being sorted in non- descending order and having the same length as the original sequence:
33333 3
Although it satisfies the spec, it is also most certainly not what was meant! This example is based on an error taken from real production code (fortu- nately caught before it was released), where a simple slip of a keystroke or a momentary lapse of reason led to an elaborate mechanism for populating the whole result with the first element of the given array.
The full postcondition is that the result is sorted and that it holds a permuta- tion of the original values. This appropriately constrains the required behavior.
162 97 Things Every Programmer Should Know

That the result length is the same as the input length comes out in the wash and doesn’t need restating.
Even stating the postcondition in the way described is not enough to give you a good test. A good test should be readable. It should be comprehensible and simple enough that you can see readily that it is correct (or not). Unless you already have code lying around for checking that a sequence is sorted and that one sequence contains a permutation of values in another, it is quite likely that the test code will be more complex than the code under test. As Tony Hoare observed:
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies and the other is to make it so com- plicated that there are no obvious deficiencies.
Using concrete examples eliminates this accidental complexity and opportu- nity for accident. For example, given the following sequence:
31415 9
The result of sorting is the following:
11345 9
No other answer will do. Accept no substitutes.
Concrete examples help to illustrate general behavior in an accessible and unambiguous way. The result of adding an item to an empty collection is not simply that it is not empty: it is that the collection now has a single item, and that the single item held is the item added. Two or more items would qualify as not empty, and would also be wrong. A single item of a different value would also be wrong. The result of adding a row to a table is not simply that the table is one row bigger; it’s also that the row’s key can be used to recover the row added. And so on.
In specifying behavior, tests should not simply be accurate: they must also be precise.


汇编test和cmp区别 看过破解教程,都知道test,cmp是比较关键,可是我一直不清楚它们究竟是怎么比较的,最后下决心找了很多资料,和大家一起把它们弄清楚. 首先看看:...
  • thanklife
  • thanklife
  • 2013年09月04日 15:57
  • 6517

go test 命令介绍

go testgo test是go语言自带的测试工具,其中包含的是两类,单元测试和性能测试 通过go help test可以看到go test的使用说明: 格式形如: go test [-c] ...
  • csapr1987
  • csapr1987
  • 2015年04月08日 12:21
  • 2031

java中 @Test注解的使用和其他成员

package com.starry.exersice; import org.junit.After; import org.junit.AfterClass; import org.junit....
  • starry7953810
  • starry7953810
  • 2017年06月12日 19:04
  • 361


spring——依赖注入DI(spring-test方式) 本文主要介绍如何实现spring 的依赖注入,并且浅显的讲述一下注入需要注意的事项。 1、创建一个maven项目 mvn archet...
  • wild46cat
  • wild46cat
  • 2017年05月01日 12:47
  • 509

go test命令参数问题

go test命令参数问题在使用go test对go代码进行单元测试的时候,遇到关于命令参数的问题,google了一下,没有找到很好的说明,其实就是一些细节而已。问题是这样的,在进行单元测试的时候,我...
  • kangaroo835127729
  • kangaroo835127729
  • 2016年03月18日 15:04
  • 3012

shell script学习(三)判断式的使用:test命令以及[ ]

shell script中的判断式,顾名思义就是判断一个式子是否满足一定的条件。在script中有如下几种实现方式: 1、使用test命令 先举个简单的例子,比如说判断在当前目录下判断一个文件scri...
  • u011321908
  • u011321908
  • 2014年01月21日 10:39
  • 4123


  • hsd2012
  • hsd2012
  • 2016年04月25日 16:08
  • 1182

经典算法问题:“this is a test” 逆序输出为“test a is this”.使用Python实现

看了http://www.dewen.org/q/13218/java++字符串的倒置排序 这个里面的回答之后,有了思路。鉴于本人刚学Python,小小练习算是。 01 ...
  • hanangellove
  • hanangellove
  • 2014年06月08日 19:21
  • 2388


test命令格式:  test condition 通常,在if-then-else语句中,用[]代替,即[ condition ]。注意:方括号两边都要用空格。   1、数值比较  ...
  • zqixiao_09
  • zqixiao_09
  • 2016年05月15日 16:32
  • 1345


TestNg提供的最基本的注解之一就是Test注解,作用在方法或者类上,此注解支持的属性有: 1、alwaysRun:提供一个false or true值,如果设置为true,则被标记的方法会永远被执...
  • haiweizhourong
  • haiweizhourong
  • 2017年02月08日 17:48
  • 663
您举报文章:Test Precisely and Concretely