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

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

问题描述

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

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 ;)

推荐答案

Alberto Savoia 的这篇散文准确地回答了这个问题(以一种非常有趣的方式!):

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 关于测试覆盖率

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

Early one morning, a programmer asked the great master:

我已准备好编写一些单元测试.我应该瞄准什么代码覆盖率为了?"

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

大师回复:

不要担心覆盖率,只需编写一些好的测试即可."

"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."

正是,"大师说.

第二个程序员微笑着鞠躬,然后离开了.

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天全站免登陆