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

2024-03-21 06:32
文章标签 规则 三条 tdd 中英对照

本文主要是介绍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 at The 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 a lot of 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前把它们重新组装起来并使它们正常工作这样的做法了。我在编程时所做的每一个决策都由一分钟后能够再次执行这样的基本需要所驱动。
 


这篇关于TDD的三条规则(中英对照)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/831993

相关文章

Adblock Plus官方规则Easylist China说明与反馈贴(2015.12.15)

-------------------------------特别说明--------------------------------------- 视频广告问题:因Adblock Plus的局限,存在以下现象,优酷、搜狐、17173黑屏并倒数;乐视、爱奇艺播放广告。因为这些视频网站的Flash播放器被植入了检测代码,而Adblock Plus无法修改播放器。 如需同时使用ads

关联规则(一)Apriori算法

此篇文章转自 http://blog.sina.com.cn/s/blog_6a17628d0100v83b.html 个人觉得比课本上讲的更通俗易懂! 1.  挖掘关联规则 1.1   什么是关联规则 一言蔽之,关联规则是形如X→Y的蕴涵式,表示通过X可以推导“得到”Y,其中X和Y分别称为关联规则的先导(antecedent或left-hand-side, LHS)和后

电子电气架构---私有总线通信和诊断规则

电子电气架构—私有总线通信和诊断规则 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节能减排。 无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事.而不是让内心的烦躁、

Python中的单下划线和双下划线使用规则

引言 Python 遵循一些关键的命名规则,这些规则涉及到使用单个或双下划线。通过这些规则,你可以在应用程序接口(API)中区分可访问的和不可访问的标识符,编写适用于继承的稳健类,以及防止命名冲突等。 遵守这些规则,你的代码将显得更贴近 Python 的风格,并且在其他 Python 程序员看来更加统一。这项技能在你为其他开发者编写代码时尤为重要。 本文[1]将带你了解: 基于使用下划线

【0324】Postgres内核 Shared Buffer Access Rules (共享缓冲区访问规则)说明

0. 章节内容 1. 共享磁盘缓冲区访问机制 (shared disk buffers) 共享磁盘缓冲区有两套独立的访问控制机制:引用计数(a/k/a pin 计数)和缓冲区内容锁。(实际上,还有第三级访问控制:在访问任何属于某个关系表的页面之前,必须持有该关系表的适当类型的锁。这里不讨论关系级锁。) Pins 在对缓冲区做任何操作之前,必须“对缓冲区pin”(即增加其引用计数, re

[mysql]SQL语言的规则和规范

规则 是什么呢,规则就是我们最基本,每时每刻都要遵守的比如人行道靠右,不能逆行, 规范 呢就是锦上添花,如果你不这么做,是不那么道德,不那么好的,就像小学生见到老师要问好,不问好可以吗,当然也是可以的,但是这样就不那么礼貌了。但是也不会开除你, 规范是建议。规则: USE dbtest2 SELECT * FROM emp 我们之前使用cmd操作的时候,是不是必须要先选择一个数据

AI聊天应用不能上架?Google play对AI类型应用的规则要求是什么?

随着生成式AI模型的广泛应用,很多开发者都有在开发AI应用或将其整合到应用中。我们知道,谷歌是非常注重应用生态的,去年开始就推出了一些针对生成式AI应用的政策,对AI应用的内容质量和合规性问题提出了一些要求。 几天前,还有开发者聊到,现在AI类型应用(如AI聊天)上架越来越难了。 (可斯信进qun与众多开发者交流上架经验) 这很可能是没了解清楚Google play 对AI应用的一些

【重学 MySQL】十二、SQL 语言的规则与规范

【重学 MySQL】十二、SQL 语言的规则与规范 基本规则注释语法规则命名规则基本命名规则具体命名规范其他注意事项 数据导入指令 SQL(Structured Query Language,结构化查询语言)的规则与规范是确保SQL语句能够正确执行、提高代码可读性和可维护性的基础。 基本规则 语句结构: SQL语句可以写在一行或多行中,为了提高可读性,建议将各子句分行

线性代数|机器学习-P33卷积神经网络ImageNet和卷积规则

文章目录 1. ImageNet2. 卷积计算2.1 两个多项式卷积2.2 函数卷积2.3 循环卷积 3. 周期循环矩阵和非周期循环矩阵4. 循环卷积特征值4.1 卷积计算的分解4.2 运算量4.3 二维卷积公式 5. Kronecker Product 1. ImageNet ImageNet 的论文paper链接如下:详细请直接阅读相关论文即可 通过网盘分享的文件:image

直接路由配置集群通信---------通过quagga 动态添加服务器路由规则

上一篇微博提到关于使用直接路由的方式配置集群通信,其优点很明确,简单易懂,但是缺点也是不容忽视的。假设一下,你的集群里面有100甚至是上千个node,在每个服务器上,你都得去配置到其他所有node的静态路由,其工作量是庞大切枯燥的。 这里我详细介绍一下,使用quagga来动态添加路由规则。软件主页http://www.quagga.net。 最简单的方式当然是直接在每台服务器上安装quagga