Google测试精华文章(1) - 测试行为,而非实现
Your trusty Calculator class is one of your most popular open source projects, with many happy users:
假设类Calculator是你最受欢迎的开源项目之一,有很多的使用者:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
You also have tests to help ensure that it works properly:
并且你还针对这个类写了些测试来确保这个类工作正常:
public void testAdd() {
assertEquals(3, calculator.add(2, 1));
assertEquals(2, calculator.add(2, 0));
assertEquals(1, calculator.add(2, -1));
}
However, a fancy new library promises several orders of magnitude speedup in your code if you use it in place of the addition operator. You excitedly change your code to use this library:
然而,有一天,你发现有一个奇特的新库(Library)能保证如果你用这个库代替以前的加法操作的话,你的代码性能提升好几个数量级。于是你欣然修改了你的代码去使用这个库:
public class Calculator {
private AdderFactory adderFactory;
public Calculator(AdderFactor adderFactory) { this.adderFactory = adderFactory; }
public int add(int a, int b) {
Adder adder = adderFactory.createAdder();
ReturnValue returnValue = adder.compute(new Number(a), new Number(b));
return returnValue.convertToInteger();
}
}
That was easy, but what do you do about the tests for this code? None of the existing tests should need to change since you only changed the code's implementation, but its user-facing behavior didn't change. In most cases, tests should focus on testing your code's public API, and your code's implementation details shouldn't need to be exposed to tests.
这很简单,但是测试代码怎么办?需要修改吗?当然不用做任何的修改,因为你只改变代码的实现,而没改变代码面向使用者的行为。在大多数情况下,测试应该只专注于测试你代码的公共API,而代码的具体实现应该是隐藏的,不应该被测试。
Tests that are independent of implementation details are easier to maintain since they don't need to be changed each time you make a change to the implementation. They're also easier to understand since they basically act as code samples that show all the different ways your class's methods can be used, so even someone who's not familiar with the implementation should usually be able to read through the tests to understand how to use the class.
测试独立于实现会使得其更容易去维护,因为当你只是修改功能的实现时,不用每次都去改case。而且这样的测试也更容易理解,因为这些测试基本上可以当作你的Code samples,能够展示使用你的类的方法的所有方式,所以即使有人不熟悉你的代码实现,也能通过读你的测试用例来理解如何使用这些类。
There are many cases where you do want to test implementation details (e.g. you want to ensure that your implementation reads from a cache instead of from a datastore), but this should be less common since in most cases your tests should be independent of your implementation.
当然也确实有很多Cases想要测试具体代码的实现细节(比如:你想要确保你确实是从一个Cache里读数据而不是从Datastore里),但是这应该不太常见因为在大多数情形里你的测试应该独立于你的具体实现。
Note that test setup may need to change if the implementation changes (e.g. if you change your class to take a new dependency in its constructor, the test needs to pass in this dependency when it creates the class), but the actual test itself typically shouldn't need to change if the code's user-facing behavior doesn't change.
注意Test Setup可能会跟着实现改变(比如,如果你修改你的类让其依赖于它的构造函数,那么在创建这个类时,你就得在你的测试里传入这个依赖对象), 但是实际上,只要代码对于使用者来说行为不变,那么Test也就不用变。
原文地址
Testing on the Toilet: Test Behavior, Not Implementation
如果您看了本篇博客,觉得对您有所收获,请点击下面的 [推荐]
如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
PS:欢迎大家加PowerMock交流群(),一起交流讨论~
Google测试精华文章(1) - 测试行为,而非实现的更多相关文章
- 《Google软件测试之道》测试工程师
愿和我一样读过这本书的人有所共鸣或者启发,愿没读过这本书的人,能获得一点点收获... 说到软件测试工程师,首先我们需要明白一个问题,软件测试工程师的职责是什么? 关于这个话题,不同的人有不同的定义:抛 ...
- Google推出iOS功能性UI测试框架EarlGrey
经过了一段时间的酝酿后,Google很高兴地宣布了EarlGrey,一款针对于iOS的功能性UI测试框架.诸如YouTube.Google Calendar.Google Photos.Google ...
- 《Google软件测试之道》测试开发工程师
拖延了将近半年的草稿,断断续续的写完了.之前草草翻看完这本书,关注点主要在TE上,而关于SET的部分则只是浏览,最近后知后觉,又翻出了这本书,重新看了一遍,又有新收获. 就说说Google的SET是如 ...
- Google+ 团队的 Android UI 测试
https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9 ...
- "Unity测试系列"文章索引
对Unity各种API的细节进行测试 Common 一些Unity基础操作的性能测试 Animation/Animator Animation Play/Stop测试 关于Animation动画事件的 ...
- Web 前端开发人员和设计师必读精华文章【系列二十六】
<Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十】
<Web 前端开发精华文章推荐>2013年第八期(总第二十期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...
- Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】
<Web 前端开发精华文章推荐>2014年第6期(总第27期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- 【圣诞特献】Web 前端开发精华文章推荐【系列二十一】
<Web 前端开发精华文章推荐>2013年第九期(总第二十一期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 ...
随机推荐
- 【转】jQuery列表拖动排列-jquery list dragsort插件参数和使用方法
转自:http://www.itokit.com/2014/0820/75058.html 我们在编辑页面元素排序的时候,我推荐使用jquery插件:dragsort. dragsort官网地址:ht ...
- (hdu step 6.3.1)Strategic Game(求用最少顶点数把全部边都覆盖,使用的是邻接表)
题目: Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 树莓派做AP发射wifi(RTL8188CUS芯片) 分类: shell ubuntu Raspberry Pi 2014-11-29 01:25 822人阅读 评论(0) 收藏
最近在做一个项目,需要用树莓派作为AP发射wifi,对比cubieboard,树莓派的配置容易得多,而且支持也更多. 较为官方的介绍配置为无线热点的文章莫过于这一篇<RPI-Wireless-H ...
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...
- MapReduce实战:查找相同字母组成的单词
1.项目需求 一本英文书籍包含成千上万个单词,现在我们需要在大量的单词中,找出相同字母组成的所有单词 2.数据集 下面是一本英文书籍截取的一部分单词内容(书籍内容是随意写的,主要目的是实现这种需求) ...
- Android进阶笔记03:Android应用中实现查看"附近的人"的功能
1. 要实现" 附近的人" 这功能,然后就研究了下: (1)首先要做的就是要获取到自己当前位置的经纬度(编程获取手机GPS定位模块的信息,进而获取自己当前位置的经纬度) (2)然后 ...
- css选择器优化
css选择器优化@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@impor ...
- date和long的相互转换
import java.text.SimpleDateFormat; import java.util.Date; public class T { public static void main(S ...
- 从创建webservice到发布webservice的一些相关总结
最近做了一个web服务,开始什么也不懂,就在网上到处找,对于刚毕业的我,感觉没用实际代码经过自己的手写出来,看什么都一头雾水,然后就看到很多人说webservice已经融入WCF..然后就先创建了WC ...
- Gprinter Android SDK V2.0 使用说明
佳博特约经销商,此店购买的打印机问题优先解决哟 https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.V1p ...