Testivus On Test Coverage

来源:互联网 发布:淘宝分销商招募书 编辑:程序博客网 时间:2024/06/07 22:18

原文:点这个链接

Testivus On Test Coverage

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

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

大师回答说:

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

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


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

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

我该放多少米呢?

程序员迷茫的回答道:

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

“太对了”,大师说。

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


那天快下班时,第三个程序员找大师问了同样的问题。

妈蛋,覆盖率得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.

原创粉丝点击