注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

代码覆盖率:80%,不能 (Code coverage goal: 80% and no less!)  

2013-01-25 11:37:12|  分类: 饭后闲侃 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一大早,一个年轻的程序员问大师:

“我准备写一些单元测试用例。代码覆盖率应该达到多少为好?”

大师回答道:

“不要考虑代码覆盖率,只要写出一些好的测试用例即可。”

年轻的程序员很高兴,鞠躬,离去。

之后没多久,第二个程序员问了大师同样的问题。

大师指着一锅烧沸的水说:

“我应该往这个锅里放多少米?”

这个程序员看起来被难住了,回答道:

“我怎么会有答案?这取决于要给多少人吃,他们饿不饿,有什么菜,你有多少米,等等。”

完全正确,” 大师说。

第二个程序员很高兴,鞠躬,离去。

末了,来了第三个程序员问了大师同样的关于代码覆盖率的问题。

“百分之八十,不能少!” 大师一拳锤在桌子上,用严厉的口气回答道。

第三个程序员很高兴,鞠躬,离去。

回复完这个之后,一个年轻的实习生走到大师身边:

“大师,今天我无意中听到了你对同一个代码覆盖率问题给出了三个不同的答案。为什么?”

大师从椅子上站起来:

“给我泡点新茶,我们聊聊这个。”

当杯子里倒满了冒着热气的绿茶后,大师开始说:

“这第一个程序员是个新手,刚刚开始学测试。目前他有大量的程序都没有测试用例。他有很长的路要走;现在对他要求代码覆盖率只会打击他,没有什么用处。最好是让他慢慢的学会写一些测试用例,测试一下。他可以以后再考虑代码覆盖率。”

“而这第二个程序员,不论对编程还是测试都是十分的有经验。我以问作答,问她应该往锅里放多少米,使她明白决定测试用例多少的因素有很多,她比我更知道这些因素——毕竟是她自己的代码。对这个问题没有一个简单的、直接的答案。以她的聪明完全能明白这个道理,正确的完成任务。”

“我明白了,” 年轻的实习生说, “但是如果没有一个简单直接的答案,那你为什么告诉第三个程序员‘百分之八十,不能少’呢?”

大师笑的前仰后合,绿茶都喷了出来。

“这第三个程序员只想得到一个简单的答案——即使根本没有简单的答案 … 而且即使有答案她也不会按答案做。”

年轻的实习生和头发斑白的大师在沉思中喝完茶。




by Alberto Savoia
I first posted this article a few years ago on the Artima Developer website; but the question of what's adequate code coverage keeps coming up, so I thought it was time for a repost ofTestivus wisdom on the subject.


Testivus on Test Coverage
Early one morning, a young 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 young 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:
“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 experienced 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 tell 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.

  1. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    I think our industry would benefit to have more of the first two people and a whole lot less of the third.

    Reply
  2. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Is this about developers or testers? The story is nice though. But again it looks like the testing is only scripting in your place.

    Reply
  3. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    I would say to the experienced dev: 

    "Aim for an increasing trend in code coverage"

    Reply
  4. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    The Way of Testivus is the best thing I've seen in a long time. Well done.

    Reply
  5. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    The story is definitely not just about code coverage :)

    Reply
  6. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页
  7. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Just give the consultant answer, "It depends..." :)

    Reply
  8. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Hope that I have a great master who can drink some tea with me. ;)

    Reply
  9. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    We need more great masters 

    Basharat

    Reply
  10. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页
  11. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页
  12. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Dear Alberto,

    I would like to translate your post to portuguese and post it on our website, because I have a few coleagues that aren't comfortable reading in english and would benefit from your post. Can I do it?

    Respectfuly,
    Júlio

    Reply
  13. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    I really like the managers attitude. I wish people would customize to help people with various skills and experience insead of assuming that one set of instructions will get us similar results. This note is really about master understanding his people.

    Reply
  14. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Cool...Master

    I guess it is a nice way to let people understand about the code coverage. Or any other question.

    I guess this is about the attibute of a person how we react in our day to day job.

    Regarding the Code Coverage. I guess the first answer was quite important. Just try to concentrate on writing the good test, instead of worring about the coverage. It is important to dissuss those test with bussiness, through that a person should be able to figure out the coverage achieved in the test.

    But nice and cool approach. :)

    Reply
  15. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Really nice story! Perfect introduction to code coverage for developers and project managers in particular!

    Reply
  16. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    >I would like to translate your post to portuguese and post it on our website ... Can I do it?<

    Hi Julio, no problem. I'd love to see Testivus in Portuguese or any other language. Thanks, Alberto

    Reply
  17. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页
    Alberto SavoiaAugust 6, 2010 at 7:11:00 AM PDT

    This comment has been removed by the author.

    Reply
  18. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Look into T.D.D., XUnit, and other pre-cog code coverage methodologies that inherently strive for 100% coverage.

    Reply
  19. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    This story has earned me huge amount of repu points on StackOverflow: http://stackoverflow.com/questions/90002/what-is-a-reasonable-code-coverage-for-unit-tests-and-why/90021#90021

    It remains to be one of the most excellent answers to the code coverage issue.

    Reply
  20. 代码覆盖率:80,不能  Code coverage goal: 80 and no less! - 和申 - 和申的个人主页

    Great story! Love it! 
    Published a german translation here: http://itscertainlyuncertain.blogspot.de/2013/01/testivus-zum-thema-test-coverage.html

    Reply

摘自:http://googletesting.blogspot.com/2010/07/code-coverage-goal-80-and-no-less.html

  评论这张
 
阅读(732)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016