Tuesday, July 20, 2010

Code coverage goal: 80% and no less!

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.
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. I think our industry would benefit to have more of the first two people and a whole lot less of the third.

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

  3. I would say to the experienced dev:

    "Aim for an increasing trend in code coverage"

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

  5. The story is definitely not just about code coverage :)

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

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

  8. 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?


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

  10. 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. :)

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

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

  13. This comment has been removed by the author.

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

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

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

  17. 100% only! Otherwise people getting squeezed for time will cover getters and setters and leave the tasty stuff.


The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.