If I get stuck and I don’t know how a complex algorithm should work I’ll write a test for an error case. Then I’ll write a test for the simplest non-error case I can think of and return a hard coded value. Then I’ll write another test case and see if I can figure out the algorithm at that point. In doing so I gain some momentum and perhaps some insight in how the algorithm should behave on an edge case and a few normal cases.
This is called triangulation and it was used in celestial navigation for thousands of years. It is easier to see you are moving when you compare your position to two or more points on the horizon rather than just one. The same applies to coding; it is often easier to figure out the behavior of an algorithm by examining a couple of test cases instead of just one.
Triangulation is a valuable problem solving technique in TDD but it can also create its own problems. Once we figure out what we need from multiple tests they no longer serve any purpose and they become redundant tests. Redundant tests are tests that do not create any new distinctions.
If we recognize that redundancy in code is a maintenance issue then we should also see that redundancy in tests is also a maintenance issue. The purpose of QA is to try to break code to verify that it is robust. The purpose of TDD is to express the intent of code in as few tests as possible. Therefore, part of refactoring a system is removing redundant tests.
Triangulation
《测试驱动开发》热门书评
-
影响你最快最久的书
12有用 0无用 元创 2006-04-18
从来没有一本书如此大影响我,这本书对我的影响有两个。一、改变了我写程序的方法。不仅立即(一周)而且可能永远改变了我写程序的习惯。二、让我开始学习和写python程序。这是一本天才写的书,开创了新的软件方法论。这是一本200页的薄书,但以前所有软件工程的几十万页的书加在一起,应该也只与这本书的份量相当...
-
《测试驱动开发》读后感
9有用 0无用 LK 2009-11-27
这本书的作者是“极限编程”之父Kent Beck,书写得很薄,每一章都只有几页的长度。这就好像用TDD方法写出的代码般,每一个单元都是精巧明快的,使得人们很容易读懂,也使人们更有意愿将其读懂(短的文章更能...
-
还不错,可以读读
5有用 0无用 Alex 2010-01-02
生动简单的例子和清晰详细的解释分析,读这本书时绝对不会感到有什么困难。个人认为,其实不用把这本书当成一本纯粹的技术书来阅读,大可以当成一本改变思考方式的技术八卦看看,也许在开发中你还是不会使用TDD(test-driven development),但是用一种极为轻松的方式系统地了解一下TDD,也是...
-
深入浅出,薄书读厚
3有用 0无用 Jarod 2006-09-02
关于测试驱动开发有很多谬论和误解。关于这点的澄清永远没有尽头,就像任何其他的方法一样,所谓正解和误解都是相伴而生的。而本书是总结这个在开发社团里面实践经验的开山之作,关于他的评价是,误解的不想读,不误解的也不愿意读,前者是因为已经有误解的心态对于这种小题目而言,实在经不起这种误解的心态去读,对于后者...
-
测试驱动更多体现的是一种思想
2有用 2无用 keni 2009-03-30
我们并不一定要遵循测试驱动的步骤,因为在n多的公司根本完全无法实施。但是我们可以以测试驱动的思想作为我们思考问题的方法。...
书名: 测试驱动开发
作者: [美] Kent Beck
出版社: 中国电力出版社
原作名: Test Driven Development: By Example
译者: 孙平平 | 张小龙
出版年: 2004-4-1
页数: 184
定价: 28.00元
装帧: 平装(无盘)
丛书: 大师签名系列
ISBN: 9787508321738