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. ListBoxControl 删除选择的项的方法

    public void RemoveSelectItems<T>(BaseListBoxControl listControl)        {            var list ...

  2. c#委托和事件(下) 分类: C# 2015-03-09 08:42 211人阅读 评论(0) 收藏

    C#中的委托和事件(下) 引言 如果你看过了 C#中的委托和事件 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(20)-权限管理系统-根据权限获取菜单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(20)-权限管理系统-根据权限获取菜单 不知不觉到20讲,真是漫长的日子,可惜最近工作挺忙,要不可以有更多 ...

  4. iOS IAP教程

    1. 创建应用 首先进入iTunes Connect然后按下 Manage Your Applications 接下来按下Add New Applicationbutton创建应用 2. 在应用中创建 ...

  5. InstallShield 12 制作安装包

    目  录 一. 二. 三. (一) 打开project... 2 (二) project助手页面... 3 1.Application Information:程序信息... 4 2.Installa ...

  6. CMAKE 学习

    http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html

  7. A different twist on pre-compiling JSPs--reference

    I’ve blogged about this topic earlier and expressed my frustrations as to how web containers don’t p ...

  8. iOS开发之支付宝集成

    项目中要用到支付功能,需要支付宝,微信,银联三大支付,所以打算总结一下,写两篇文章,方便以后的查阅, 大家在做的时候也能稍微参考下,用到的地方避免再次被坑.这是第二篇支付宝集成,第一篇银联支付在这里. ...

  9. iOS UIKit:viewController之Present (3)

    弹出和转换view controller技术是一种快速且简单的方式将新view content展示在屏幕中.目前有两种方式弹出新的view controller:Present方式和segues方式. ...

  10. Ubuntu16.04LTS安装

    1. 制作u盘启动盘 下载ubuntu-16.04-desktop-amd64.iso文件后,使用u盘启动盘制作工具:Win32DiskImager(14.04LTS后都需要用到这工具制作:https ...