TDD的三条规则(中英对照)

The Three Rules of TDD.
TDD的三条规则
邓辉 译

Over the years I have come to describe Test Driven Development in terms of three simple rules. They are:
这些年来, 我喜欢用下面三条简单的规则来描述测试驱动开发:
  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
1 、除非为了使一个失败的unit test通过,否则不允许编写任何产品代码
  1. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
2 .在一个单元测试中只允许编写刚好能够导致失败的内容(编译错误也算失败)
  1. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
3 、只允许编写刚好能够使一个失败的unit test通过的产品代码

You must begin by writing a unit test for the functionality that you intend to write. But by rule 2,you can't write very much of that unit test. As soon as the unit test code fails to compile,or fails an assertion,you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass,and no more.
对于任何功能,都必须以编写针对该项功能的unit test开始。根据规则2,在unit test中,你不能编写太多的内容。只要一出现该unit test代码不能编译通过,或者断言失败,就必须停下来开始编写产品代码。根据规则3,你所编写的产品代码应该以刚好能够使得unit test编译或者测试通过为准。

If you think about this you will realize that you simply cannot write very much code at all without compiling and executing something. Indeed,this is really the point. In everything we do,whether writing tests,writing production code,or refactoring,we keep the system executing at all times. The time between running tests is on the order of seconds,or minutes. Even 10 minutes is too long.
仔细想想,就会发现如果不是为了编译或者执行某些东西,你根本不能编写任何代码。事实上,这正是关键所在。我们在做任何事情时(无论是写测试、写产品代码还是重构),都要保证系统能够一直运行。运行测试的间隔时间是秒或者分钟级的。即使是10分钟都太长了。

Too see this in operation,take a look atThe Bowling Game Kata.
如果想了解实际的操作过程,可以看看“The Bowling Game Kata
(译者注: Kata是目前北美和欧洲一些领先的软件咨询公司开创的一种用于 掌握 软件开发技能的手段,类似于武术中的 招式。目的就是试图寻找出软件开发中的一些 招式,让学习者可以不断演练,从而打下一个良好的基础。


Now most programmers,when they first hear about this technique,think: "This is stupid!" "It's going to slow me down,it's a waste of time and effort,It will keep me from thinking,it will keep me from designing,it will just break my flow." However,think about what would happen if you walked in a room full of people working this way. Pick any random person at any random time. A minute ago,all their code worked.
目前有很多程序员,当他们第一次听到这种技术时都会认为:“这种做法太愚蠢了!”。“这种方法会降低我的开发速度,这样做就是时间和精力的浪费,它会让我无法思考,无法设计,它会打断我的思路。”不过,请试着想一想,当你走进一个坐满了以这种方式工作的人的房间时,会发生怎样的情况。随时、随意选一个人,在一分钟前,他们的代码都是可以工作的。

Let me repeat that:A minute ago all their code worked!And it doesn't matter who you pick,and it doesn't matter when you pick.A minute ago all their code worked!
请让我再重复一遍:一分钟前,每个人的代码都能够工作!不管你选哪个人,不过你在何时去选。一分钟前,每个人的代码都能够工作!

If all your code works every minute,how often will you use a debugger? Answer,not very often. It's easier to simply hit ^Z a bunch of times to get the code back to a working state,and then try to write the last minutes worth again. And if you aren't debugging very much,how much time will you be saving? How much time do you spend debugging now? How much time do you spend fixing bugs once you've debugged them? What if you could decrease that time by a significant fraction?
如果你的所有代码自始至终都可以工作,那么你会频繁使用调试器呢?答案显然是:不频繁。轻松地按几下^Z就可以容易地使代码返回到一个正常工作的状态,接下来把几分钟前的工作重新做一遍即可。如果你不常进行调试,会节省多少时间呢?而现在你花在调试上的时间又有多少呢?在调试完后,你又花了多少时间来修正bug呢?如果你能够大大减少这些时间会怎么样呢?

But the benefit goes far beyond that. If you work this way,then every hour you are producing several tests. Every day dozens of tests. Every month hundreds of tests. Over the course of a year you will write thousands of tests. You can keep all these tests and run them any time you like! When would you run them? All the time! Any time you made any kind of change at all!
好处还不只如此。如果你采用这种方法,那么你每小时都会编写几个测试。一天就是数十个。一个月就是数百个。一年下来,你所编写的测试就会有数千个。你可以保持这些测试并且随时都可以运行它们。什么时候运行它们呢?始终运行!只要你进行了更改,就去运行它们。

Why don't we clean up code that we know is messy? We're afraid we'll break it. But if we have the tests,we can be reasonably sure that the code is not broken,or that we'll detect the breakage immediately. If we have the tests we become fearless about making changes. If we see messy code,or an unclean structure,we can clean it without fear. Because of the tests,the code becomes malleable again. Because of the tests,software becomes soft again.
有些代码我们知道已经混乱不堪了,可是为何不去清理它们呢?因为我们害怕这样做会造成破坏。但是如果我们拥有测试,我们就可以合理的肯定代码没有被破坏,因为测试可以即时地把破坏的地方检测出来。我们看到混乱的代码或者含糊的结构时,我们就可以毫无担心地清理它。因为有了测试,代码重新变得可塑。因为有了测试,软件重新变软了。

But the benefits go beyond that. If you want to know how to call a certain API,there is a test that does it. If you want to know how to create a certain object,there is a test that does it. Anything you want to know about the existing system,there is a test that demonstrates it. The tests are like little design documents,little coding examples,that describe how the system works and how to use it.
好处还不只如此。如果你想知道如何去调用一个特定API,会有一个测试告诉你如何做。如果你想知道如何去创建一个特定对象,会有一个测试告诉你如何去做。关于现有系统你想知道的任何事,都会有一个测试演示给你看。测试就像是小型的设计文档,小型的代码样例,描述了系统的工作和使用方式。

Have you ever integrated a third party library into your project? You got a big manual full of nice documentation. At the end there was a thin appendix of examples. Which of the two did you read? The examples of course! That's what the unit tests are! They are the most useful part of the documentation. They are the living examples of how to use the code. They are design documents that are hideously detailed,utterly unambiguous,so formal that they execute,and they cannot get out of sync with the production code.
你曾经在项目中集成过第三方库吗?你拿到一本相关的厚厚的精致的文档手册,在手册末尾是一沓薄薄的样例附录。你会先看手册的哪一部分呢?当然是样例部分!那些就是unit tests。它们是文档中最为有用的内容。它们是使用代码的真实例子。它们是极其详细、完全没有歧义的设计文档,它们是如此的正规以至于可以运行。它们根本不会和产品代码失去同步。

But the benefits go beyond that. If you have ever tried to add unit tests to a system that was already working,you probably found that it wasn't much fun. You likely found that you either had to change portions of the design of the system,or cheat on the tests; because the system you were trying to write tests for was not designed to be testable. For example,you'd like to test some function 'f'. However,'f' calls another function that deletes a record from the database. In your test,you don't want the record deleted,but you don't have any way to stop it. The system wasn't designed to be tested.
好处还不只如此。如果你曾经为一个已经工作着的系统增加unit tests,你会发现那一点都不好玩。其中,很可能会碰到这样的情形:要么就必须更改系统某个部分的设计,要么就得采用一些欺骗的手段让测试通过,这是因为这个系统本身没有被设计成可测试的。例如:你想测试某个函数f。但是f又调用了另外一个会从数据库中删除一条记录的函数。在测试时,你不希望记录被删除掉,但是你却没有任何阻止的方法。因为系统没有被设计成可测试的。


When you follow the three rules of TDD,all your code will be testable by definition!And another word for "testable" is "decoupled". In order to test a module in isolation,you must decouple it. So TDD forces you to decouple modules. Indeed,if you follow the three rules,you will find yourself doing much more decoupling than you may be used to. This forces you to create better,less coupled,designs.
当你遵循TDD的3条规则时,你的所有代码天生就是可测试的。而“可测试”又意味着“解耦”。为了隔离地测试一个模块,必须要对它进行解耦合。因此,TDD迫使你对模块进行解耦。事实上,如果遵循了这3条规则,就会发现你所做的解耦工作要比以前多得多。而这又推动你创建出更好,耦合更小的设计。


Given all these benfits,these stupid little rules of TDD might not actually be so stupid. They might actually be something fundemental,something profound. Indeed,I had been a programmer for nearly thirty years before I was introduced to TDD. I did not think anyone could teach me a low level programming practice that would make a difference. Thirty years is alotof experience after all. But when I started to use TDD,I was dumbfounded at the effectiveness of the technique. I was also hooked. I can no longer concieve of typing in a big long batch of code hoping it works. I can no longer tolerate ripping a set of modules apart,hoping to reassemble them and get them all working by next Friday. Every decision I make while programming is driven by the basic need to be executing again a minute from now.
具有这么多好处,这些愚蠢的TDD小规则实际上也许并不愚蠢。它们应该是一些基础性的、意义深远的东西。事实上,在接触TDD前,我已经有近30年的编程经验。我不认为有谁会教我一个低层次的、有实质性不同的编程实践。毕竟30年意味着很多的经验。但是当我开始使用TDD时,这种技术的有效性使我惊呆并使我沉迷其中。我再也无法想象键入一长串代码并期望它能够工作这样的事情了。同时,我再也无法容忍把一系列模块打散,期望到下周5前把它们重新组装起来并使它们正常工作这样的做法了。我在编程时所做的每一个决策都由一分钟后能够再次执行这样的基本需要所驱动。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


什么是设计模式一套被反复使用、多数人知晓的、经过分类编目的、代码 设计经验 的总结;使用设计模式是为了 可重用 代码、让代码 更容易 被他人理解、保证代码 可靠性;设计模式使代码编制  真正工程化;设计模式使软件工程的 基石脉络, 如同大厦的结构一样;并不直接用来完成代码的编写,而是 描述 在各种不同情况下,要怎么解决问题的一种方案;能使不稳定依赖于相对稳定、具体依赖于相对抽象,避免引
单一职责原则定义(Single Responsibility Principle,SRP)一个对象应该只包含 单一的职责,并且该职责被完整地封装在一个类中。Every  Object should have  a single responsibility, and that responsibility should be entirely encapsulated by t
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强烈推荐。原文截图*************************************************************************************************************************原文文本************
适配器模式将一个类的接口转换成客户期望的另一个接口,使得原本接口不兼容的类可以相互合作。
策略模式定义了一系列算法族,并封装在类中,它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,它是针对软件开发中经常遇到的一些设计问题,总结出来的一套通用的解决方案。
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
迭代器模式提供了一种方法,用于遍历集合对象中的元素,而又不暴露其内部的细节。
外观模式又叫门面模式,它提供了一个统一的(高层)接口,用来访问子系统中的一群接口,使得子系统更容易使用。
单例模式(Singleton Design Pattern)保证一个类只能有一个实例,并提供一个全局访问点。
组合模式可以将对象组合成树形结构来表示“整体-部分”的层次结构,使得客户可以用一致的方式处理个别对象和对象组合。
装饰者模式能够更灵活的,动态的给对象添加其它功能,而不需要修改任何现有的底层代码。
观察者模式(Observer Design Pattern)定义了对象之间的一对多依赖,当对象状态改变的时候,所有依赖者都会自动收到通知。
代理模式为对象提供一个代理,来控制对该对象的访问。代理模式在不改变原始类代码的情况下,通过引入代理类来给原始类附加功能。
工厂模式(Factory Design Pattern)可细分为三种,分别是简单工厂,工厂方法和抽象工厂,它们都是为了更好的创建对象。
状态模式允许对象在内部状态改变时,改变它的行为,对象看起来好像改变了它的类。
命令模式将请求封装为对象,能够支持请求的排队执行、记录日志、撤销等功能。
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。 基本介绍 **意图:**在不破坏封装性的前提下,捕获一个对象的内部状态,并在该
顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为
享元模式(Flyweight Pattern)(轻量级)(共享元素)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结