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) - 测试行为,而非实现的更多相关文章

  1. 《Google软件测试之道》测试工程师

    愿和我一样读过这本书的人有所共鸣或者启发,愿没读过这本书的人,能获得一点点收获... 说到软件测试工程师,首先我们需要明白一个问题,软件测试工程师的职责是什么? 关于这个话题,不同的人有不同的定义:抛 ...

  2. Google推出iOS功能性UI测试框架EarlGrey

    经过了一段时间的酝酿后,Google很高兴地宣布了EarlGrey,一款针对于iOS的功能性UI测试框架.诸如YouTube.Google Calendar.Google Photos.Google ...

  3. 《Google软件测试之道》测试开发工程师

    拖延了将近半年的草稿,断断续续的写完了.之前草草翻看完这本书,关注点主要在TE上,而关于SET的部分则只是浏览,最近后知后觉,又翻出了这本书,重新看了一遍,又有新收获. 就说说Google的SET是如 ...

  4. Google+ 团队的 Android UI 测试

    https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9 ...

  5. "Unity测试系列"文章索引

    对Unity各种API的细节进行测试 Common 一些Unity基础操作的性能测试 Animation/Animator Animation Play/Stop测试 关于Animation动画事件的 ...

  6. Web 前端开发人员和设计师必读精华文章【系列二十六】

    <Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  7. Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十】

    <Web 前端开发精华文章推荐>2013年第八期(总第二十期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...

  8. Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】

    <Web 前端开发精华文章推荐>2014年第6期(总第27期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  9. 【圣诞特献】Web 前端开发精华文章推荐【系列二十一】

    <Web 前端开发精华文章推荐>2013年第九期(总第二十一期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和  ...

随机推荐

  1. Java Web中资源的访问路径

    在web应用中,以“/”开头的是绝对路径,不以“/”开头的是相对路径.   在服务器端,通常都使用绝对路径.例如web.xml.struts.xml.servlet等的访问路径都是以“/”开始. 服务 ...

  2. mac下versions 提交提示 SVN Working Copy xxx locked

    终端进入工程目录,执行 find . | grep ".svn/lock" | xargs rm

  3. 谈一下怎样设计Oracle 分区表

    在谈设计Oracle分区表之间先区分一下分区表和表空间的个概念: 表空间:表空间是一个或多个数据文件的集合,全部数据对象都存放在指定的表空间中,但主要存放表,故称表空间. 分区表:分区致力于解决支持极 ...

  4. 排序算法c语言描述---堆排序

    排序算法系列学习,主要描述冒泡排序,选择排序,直接插入排序,希尔排序,堆排序,归并排序,快速排序等排序进行分析. 文章规划: 一.通过自己对排序算法本身的理解,对每个方法写个小测试程序.具体思路分析不 ...

  5. 树莓派做AP发射wifi(RTL8188CUS芯片) 分类: shell ubuntu Raspberry Pi 2014-11-29 01:25 822人阅读 评论(0) 收藏

    最近在做一个项目,需要用树莓派作为AP发射wifi,对比cubieboard,树莓派的配置容易得多,而且支持也更多. 较为官方的介绍配置为无线热点的文章莫过于这一篇<RPI-Wireless-H ...

  6. [Flux] 2. Overview and Dispatchers

    Flux has four major components: Stores, Dispatchers, Views, and Actions. These components interact l ...

  7. [转] Scale-up 和Scale-out的区别

    http://www.cnblogs.com/spork/archive/2009/12/29/1634766.html 来自原小站,曾经迷糊过的东西,表上来,希望对正在迷糊或即将迷糊的人有帮助. 谈 ...

  8. android开发之this.finish()的使用 分类: android 学习笔记 2015-07-18 19:05 30人阅读 评论(0) 收藏

    在一个Activity用完之后应该将之finish掉,但是,之前在学校里自己摸索着开发时并没有太注意这个问题,因为activity无论是否finish掉对功能的影响貌似都不是那么明显(这是读书时候的观 ...

  9. Java_Activiti5_菜鸟也来学Activiti5工作流_之JUnit单元测试(四)

    /**ActivitiSpringJuinitTest.java * author : 冯孟活 ^_^ * dates : 2015年9月2日 下午2:16:54 * class : activiti ...

  10. C#内存修改

    先通过 System.Diagnostics.Process类获取想要编辑的进程 调用API [Flags]                    public enum ProcessAccessT ...