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 Precisely and Concretely的更多相关文章

  1. RFC3261--sip

    本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...

  2. react programming

    So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...

  3. Learning to Promote Saliency Detectors

    Learning to Promote Saliency Detectors 原本放在了思否上, 但是公式支持不好, csdn广告太多, 在博客园/掘金上发一下 https://github.com/ ...

  4. 深度学习Deep learning

    In the last chapter we learned that deep neural networks are often much harder to train than shallow ...

  5. IDL build

    For Developers‎ > ‎Design Documents‎ > ‎ IDL build 目录 1 Steps 2 GYP 3 Performance 3.1 Details ...

  6. [C2P1] Andrew Ng - Machine Learning

    About this Course Machine learning is the science of getting computers to act without being explicit ...

  7. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  8. 2017年计算语义相似度最新论文,击败了siamese lstm,非监督学习

    Page 1Published as a conference paper at ICLR 2017AS IMPLE BUT T OUGH - TO -B EAT B ASELINE FOR S EN ...

  9. ASP.NET Core project.json imports 是什么意思?

    示例代码: "frameworks": { "netcoreapp1.0.0": { "imports" : "portable- ...

随机推荐

  1. arp与免费arp的差别,arp老化

    免费arp:应用场景: case1:PC通过DHCP申请地址.在获取到IP地址后,会发送免费ARP,目的用于探測同一网段时候存在同样的IP地址终端,防止IP冲突. case2:PC的MAC地址发生变化 ...

  2. [Angular] ngx-formly (AKA angular-formly for Angular latest version)

    In our dynamic forms lessons we obviously didn’t account for all the various edge cases you might co ...

  3. 串口通讯 ADC0804 数码管

    #include<reg52.h> #include<stdio.h> #include<intrins.h> #define uchar unsigned cha ...

  4. JSP页面标签

    1.EL表达式中empty的用法 EL表达式中empty的用法 <c:if test="${! empty key}">${key}</c:if> < ...

  5. hive2.0函数大全

    Hive2.0函数大全(中文版)   摘要 Hive内部提供了很多函数给开发者使用,包括数学函数,类型转换函数,条件函数,字符函数,聚合函数,表生成函数等等,这些函数都统称为内置函数. 目录 数学函数 ...

  6. PHP验证器类Validator

    Particle\Validator是一个小巧优雅的实用的PHP验证类库,提供了一个非常简洁的API.它无需依赖其他组件,提供友好的文档,并且有利于扩展. 安装 composer require pa ...

  7. P2216 [HAOI2007]理想的正方形(二维RMQ)

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  8. android studio开发去掉titlebar

    android:theme="@style/AppTheme"换成android:theme="@style/Theme.AppCompat.NoActionBar&qu ...

  9. ffmpeg常用指令

    在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundation -list_devices true -i "" -f 指定的是输入输出格式, -i指定输入的 ...

  10. ZBrush中Blob点滴笔刷介绍

    对于ZBrush®来说,笔刷的使用时至关重要的,ZBrush中给我们提供了越来越多的笔刷的同时,我们也要根据经验来合理选择笔刷.本文内容小编将分享Blob点滴笔刷的简单介绍,该笔刷在使用时笔头犹如一股 ...