How much test coverage do you need? - The Testivus Answer
Referring to "The Way of Testivus" entry:
Morgan Conrad asked: "What is Testivus' wisdom concerning the proper percentage of test coverage?"
Here you go Morgan:
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 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.