翻译:测试覆盖率,应该多少比较合适?

一天早上,一个程序员问大师:

我准备要写单元测试了,请问我要达到多少覆盖率呢?

大师回答说:

不用担心覆盖率了,写出来有效的单元测试就好了。

程序员笑了,向大师鞠躬,然后愉快的去干活了。


过了一会儿,第二个程序员向大师问了同样的问题。

大师指着小米牌智能电饭锅,对程序员问道:

我该放多少米呢?

程序员迷茫的回答道:

我怎么知道呢?这处决于多少人吃饭,他们饭量多大,有多少其他的辅食,当然还得看有多少米等等。

"太对了”,大师说。

第二个程序员心领神会,鞠躬,然后蹦蹦跳跳的干活去了。


那天快下班时,第三个程序员找大师问了同样的问题。大师边拍桌子,用尖利的声音咆哮道:

妈蛋,覆盖率得80%以上,一个子都不能少!

第三个程序员得到了答案,满意的微笑,欢快的去干活去了。


等第三个程序员出去后,大师的徒弟问大师:

大师啊,今天我听你回答关于覆盖率的问题,同样的问题却有三个答案,为何呢?

大师从他的椅子上站起来,说:

给劳资倒杯茶去,且听我细细分享。

随着滚烫的绿茶冒出淡淡清香的青烟,大师开始回答了:

第一个程序员是个新手,刚刚开始搞单元测试,而且他要测试的那个代码,堆成山啊!工作量简直不忍直视啊!我估计他得加班到吐血了,现在关注覆盖率还不是时候啊,现在就提覆盖率要求,他肯定会崩溃明天就给劳资辞职了。目前先稳住他,管他多少覆盖率,先提交些测试用例再说。等测试用例多了,劳资再要求覆盖率,哈哈。

第二个程序员,写代码和测试都非常有经验,是个老鸟啊。这年头队伍不好带啊,我用电饭锅做隐喻,而且还是智能的,显得我逼格够高,让他意识到覆盖率决定于很多因素就好了,具体依赖什么他比我还清楚,那是她的代码啊,我就不一一细说了,万一说漏了很容易被老司机嘲笑的。实际上覆盖率应该是多少,乃衣无,是没有唯一和简单的答案的。

“擦,明白了”,大师的徒弟说到,“既然没有简单答案,为何你回答第三个程序员时告诉他是80%以上呢?”

大师大笑,坐回他的太师椅,留下一句话:

第三个程序员嘛,他只是想知道简单的答案,即使根本就没有简单的答案。

下面是原文:点这个链接

Testivus On Test Coverage

Early one morning, a programmer asked the great master:

I am ready to write some unit tests. What code coverage should I aim for?

The great master replied:

Don’t worry about coverage, just write some good tests.

The programmer smiled, bowed, and left.


Later that day, a second programmer asked the same question.

The great master pointed at a pot of boiling water and said:

How many grains of rice should I put in that pot?

The programmer, looking puzzled, replied:

How can I possibly tell you? It depends on how many people you need to feed, how hungry they are, what other food you are serving, how much rice you have available, and so on.

Exactly, said the great master.

The second programmer smiled, bowed, and left.


Toward the end of the day, a third programmer came and asked the same question about code coverage.

Eighty percent and no less!

Replied the master in a stern voice, pounding his fist on the table.

The third programmer smiled, bowed, and left.


After this last reply, a young apprentice approached the great master:

Great master, today I overheard you answer the same question about code coverage with three different answers. Why?

The great master stood up from his chair:

Come get some fresh tea with me and let’s talk about it.

After they filled their cups with smoking hot green tea, the great master began to answer:

The first programmer is new and just getting started with testing. Right now he has a lot of code and no tests. He has a long way to go; focusing on code coverage at this time would be depressing and quite useless. He’s better off just getting used to writing and running some tests. He can worry about coverage later.

The second programmer, on the other hand, is quite experience both at programming and testing. When I replied by asking her how many grains of rice I should put in a pot, I helped her realize that the amount of testing necessary depends on a number of factors, and she knows those factors better than I do – it’s her code after all. There is no single, simple, answer, and she’s smart enough to handle the truth and work with that.

I see, said the young apprentice, but if there is no single simple answer, then why did you answer the third programmer ‘Eighty percent and no less’?

The great master laughed so hard and loud that his belly, evidence that he drank more than just green tea, flopped up and down.

The third programmer wants only simple answers – even when there are no simple answers … and then does not follow them anyway.

The young apprentice and the grizzled great master finished drinking their tea in contemplative silence.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容