单元测试的合理代码覆盖率是多少(为什么)? [英] What is a reasonable code coverage % for unit tests (and why)?

查看:502
本文介绍了单元测试的合理代码覆盖率是多少(为什么)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要为单元测试规定最小百分比的代码覆盖率,甚至要求提交到存储库,那将是什么?



请解释您是如何得出答案的(因为如果您只做一个数字,那么我本可以自己完成;)

解决方案

阿尔贝托·萨沃亚(Alberto Savoia)的这篇散文恰好回答了这个问题(以一种非常有趣的方式!):



http://www.artima.com/forums/flat.jsp?forum=106&thread=204677


测试范围内的证言



早期一天早晨,一位程序员问
大师:



我准备编写一些单元测试。
的目标代码覆盖范围是什么?



伟大的大师回答:





程序员笑着,鞠躬,然后剩下



...



那天以后,第二个程序员
提出了同样的问题。



伟大的大师指着一锅
的沸水,然后说:



我应该在那个锅里放几粒米?



程序员看上去很困惑,
回答:



我怎么可能告诉你?这取决于您需要多少人食用b,
的食物,他们有多饿,所提供的其他
的食物,有多少饭
,等等。



是的,伟大的大师说。



第二个程序员微笑着,鞠躬,
然后走了。



...



一天快结束时,第三个
程序员来问同样的
有关代码覆盖率的问题。



百分之八十,而且不少!主人用一种严厉的声音回答,
用拳头砸在桌子上。



第三个程序员微笑着,鞠躬,
然后离开。

p>

...



在此最后答复之后,年轻的
学徒接近了不起的
大师:



大师,今天我听到您用三个不同的
答案回答有关
代码覆盖率的相同问题。为什么?



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



来吧!



在他们的杯子里装满
抽着热绿茶的杯子后,这位出色的
主人开始答案:



第一个程序员是新手,刚刚开始测试。
现在他有很多代码,没有
测试。他还有很长的路要走。
目前只专注于代码覆盖范围,
令人沮丧,而且毫无用处。
他最好还是习惯
编写和运行一些测试。他可以
稍后再担心覆盖范围。



另一方面,第二个程序员在
的编程和测试方面经验丰富。当我
询问她应该将多少谷物
放入锅中时,我
帮助她意识到需要进行
的检测量取决于数字
的因素,她比我更了解这些
的因素-毕竟是她的
代码。没有一个简单的
答案,而且她足够聪明的
能够处理事实并与
一起工作。



我明白了,年轻的学徒
说,但是,如果没有一个简单的
答案,那您为什么要回答
的第三位程序员'百分之八十,
不少于'?



大师的笑声如此之大,
大声笑到他的肚子,这表明他
喝的不仅仅是绿茶,
上下翻转。



第三位程序员只想要简单的答案-即使在没有
的简单答案的情况下……也不会$ b $

年轻的徒弟和灰心丧气的
大师在沉思的沉默中喝完了
茶。



If you were to mandate a minimum percentage code-coverage for unit tests, perhaps even as a requirement for committing to a repository, what would it be?

Please explain how you arrived at your answer (since if all you did was pick a number, then I could have done that all by myself ;)

解决方案

This prose by Alberto Savoia answers precisely that question (in a nicely entertaining manner at that!):

http://www.artima.com/forums/flat.jsp?forum=106&thread=204677

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.

这篇关于单元测试的合理代码覆盖率是多少(为什么)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆