Test Precisely and Concretely
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的更多相关文章
- RFC3261--sip
本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...
- react programming
So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...
- Learning to Promote Saliency Detectors
Learning to Promote Saliency Detectors 原本放在了思否上, 但是公式支持不好, csdn广告太多, 在博客园/掘金上发一下 https://github.com/ ...
- 深度学习Deep learning
In the last chapter we learned that deep neural networks are often much harder to train than shallow ...
- IDL build
For Developers > Design Documents > IDL build 目录 1 Steps 2 GYP 3 Performance 3.1 Details ...
- [C2P1] Andrew Ng - Machine Learning
About this Course Machine learning is the science of getting computers to act without being explicit ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- 2017年计算语义相似度最新论文,击败了siamese lstm,非监督学习
Page 1Published as a conference paper at ICLR 2017AS IMPLE BUT T OUGH - TO -B EAT B ASELINE FOR S EN ...
- ASP.NET Core project.json imports 是什么意思?
示例代码: "frameworks": { "netcoreapp1.0.0": { "imports" : "portable- ...
随机推荐
- 基于Solr的HBase实时查询方案
实时查询方案 HBase+Solr+HBase-Indexer 1.HBase提供海量数据存储 2.solr提供索引构建与查询 3.HBase indexer提供自己主动化索引构建(从HBase到So ...
- Gradle的奇妙之处
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/50465885 Google I/O 2013大会上公布了AS,现在已经发展到2.0-b ...
- Java向上转型和向下转型(附具体样例)
Java向上转型和向下转型(附具体样例) 熬夜整理的关于Java向上和向下转型的样例,很的通俗易懂哦~~~~ 一 ...
- unknown argument: '-websockets'
找到building setting找到other link flgs里把里面'-websockets删掉
- linux含有某字符串的文件
find .|xargs grep -ri "IBM" -l 只列出文件名:grep -rn "Item" * -l pattern files
- Android学习笔记(十二)——使用意图传递数据的几种方式
使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...
- [POJ 1316] 树上的询问
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...
- Java并发包
刚看到一篇总结的比较全的JUC包总结,转载如下: 1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.con ...
- NEU 1040 Count
1040: Count 时间限制: 1 Sec 内存限制: 128 MB提交: 59 解决: 23[提交][状态][讨论版] 题目描述 Many ACM team name may be very ...
- checkbox改写
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...