Code coverage goal: 80% and no less!
Tuesday, July 20, 2010
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 of Testivus 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.“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 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.”
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.
I think our industry would benefit to have more of the first two people and a whole lot less of the third.
ReplyDeleteIs this about developers or testers? The story is nice though. But again it looks like the testing is only scripting in your place.
ReplyDeleteI would say to the experienced dev:
ReplyDelete"Aim for an increasing trend in code coverage"
The Way of Testivus is the best thing I've seen in a long time. Well done.
ReplyDeleteThe story is definitely not just about code coverage :)
ReplyDeleteWe need great master...........
ReplyDeleteJust give the consultant answer, "It depends..." :)
ReplyDeleteHope that I have a great master who can drink some tea with me. ;)
ReplyDeleteWe need more great masters
ReplyDeleteBasharat
very cool. Thanks for sharing!
ReplyDeleteInteresting!
ReplyDeleteDear Alberto,
ReplyDeleteI 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
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.
ReplyDeleteCool...Master
ReplyDeleteI 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. :)
Really nice story! Perfect introduction to code coverage for developers and project managers in particular!
ReplyDelete>I would like to translate your post to portuguese and post it on our website ... Can I do it?<
ReplyDeleteHi Julio, no problem. I'd love to see Testivus in Portuguese or any other language. Thanks, Alberto
This comment has been removed by the author.
ReplyDeleteLook into T.D.D., XUnit, and other pre-cog code coverage methodologies that inherently strive for 100% coverage.
ReplyDeleteThis 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
ReplyDeleteIt remains to be one of the most excellent answers to the code coverage issue.
Great story! Love it!
ReplyDeletePublished a german translation here: http://itscertainlyuncertain.blogspot.de/2013/01/testivus-zum-thema-test-coverage.html
100% only! Otherwise people getting squeezed for time will cover getters and setters and leave the tasty stuff.
ReplyDeleteInteresting post!
ReplyDeleteTesting is a huge subject hence is subjective per se! What I aim to do mostly in my development work is to test enough for the system to pass functional and non functional requirements as required in the SRS...and more details can follow.