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中的一些常见错误

    1.空指针错误 在java数组的使用中,有时候需要对字符串数组中的元素进行对比.那么当元素不为null时,程序会正常运行:然而,一旦对比的元素为null,那么程序就会出现空指针错误. 解决方法:加入保 ...

  2. string字符串转成16进制

    package util; public class EscapeUnescape { public static String escape(String src) { int i; char j; ...

  3. Java 多线程并发 Future+callable 实例

    需求:一个业务实现 查询, 因为 要查询十几次, 所以每个平均0.6秒, 之前只有主线程一步步查 ,结果用了10秒,效率十分低下 , 于是改用线程池并发: 以下是代码设计: 1.线程池工具类: pac ...

  4. hdu2128之BFS

    Tempter of the Bone II Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 98304/32768 K (Java/ ...

  5. WebLogic: The Definitive Guide examined WebLogic's security mechanisms--reference

    reference from: http://www.onjava.com/pub/a/onjava/excerpt/weblogic_chap17/index1.html?page=1 ...... ...

  6. 旅行喵 React Native 技术实践

    旅行喵,是一款帮助用户快乐旅行的APP. 第一版的首打功能是行程定制,和景点信息介绍.大家可以在上面做非常简单的偏好选择,通过我们的智能算法生成适合自己的旅行路线. 为什么要用RN呢? 首先,相对于其 ...

  7. 第一篇:python基础

    python基础   python基础 本节内容 python起源 python的发展史 为什么选择python3 第一个python程序 变量定义 表达式和运算符 用户输入 流程控制 判断 流程控制 ...

  8. 导出到excel

    /// <summary> /// 导出 /// </summary> /// <param name="table">数据表</para ...

  9. 基于事件的异步模式——BackgroundWorker

    实现异步处理的方法很多,经常用的有基于委托的方式,今天记录的是基于事件的异步模式.利用BackgroundWorker组件可以很轻松的实现异步处理,并且该组件还支持事件的取消.进度报告等功能.本文以计 ...

  10. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...