100% Code Coverage? 36
Should you strive for 100% code coverage from your unit tests? It’s probably not mandatory and if your goal is 100% coverage, you’ll focus on that goal and not focus on writing the best tests for the behavior of your code.
That said, here are some thoughts on why I still like to get close to 100%.
- I’m anal retentive. I don’t like those little red bits in my coverage report. (Okay, that’s not a good reason…)
- Every time I run the coverage report, I have to inspect all the uninteresting cases to find the interesting cases I should cover.
- The tests are the specification and documentation of the code, so if something nontrivial but unexpected happens, there should still be a test to “document” the behavior, even if the test is hard to write.
- Maybe those places without coverage are telling me to fix the design.
I was thinking about this last point the other day when considering a bit of Java code that does a downcast (assume that’s a good idea, for the sake of argument…), wrapped in a try/catch block for the potential ClassCastException
:
public void handleEvent (Event event) throws ApplicationException { try { SpecialEvent specialEvent = (SpecialEvent) event; doSomethingSpecial (specialEvent); } catch (ClassCastException cce) { throw new ApplicationException(cce); } }
To get 100% coverage, you would have to write a test that inputs an object of a different subtype of Event
to trigger coverage of the catch block. As we all know, these sorts of error-handling code blocks are typically the hardest to cover and ones we’re most likely to ignore. (When was the last time you saw a ClassCastException
anyway?)
So my thought was this, we want 100% of the production code to be developed with TDD, so what if we made 100% coverage a similar goal? How would that change our designs? We might decide that since we have to write a test to cover this error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in this case, making sure that we test all callers and ensure that they obey the method’s ‘contract’? Should we just let the ClassCastException
fly out of the function and let a higher-level catch block handle it? After all, catching and rethrowing a different exception is slightly smelly and the code would be cleaner without the try/catch block. (For completeness, a good use of exception wrapping is to avoid namespace pollution. We might not want application layer A to know anything about layer C’s exception types, so we wrap a C exception in an A exception, which gets passed through layer B…)
100% coverage is often impossible or impractical, because of language or tool oddities. Still, if you give in early, you’re overlooking some potential benefits.
Trackbacks
Use the following link to trackback from your own site:
http://blog.objectmentor.com/articles/trackback/7523
I agree – I voiced the same opinions:
http://www.jasonbock.net/JB/Default.aspx?blog=entry.8b942f550d504b0198837636db915a79
I’d say shooting for 90% coverage is feasible, but trying to hit 100% can become a futile exercise.
D, so what if we made 100% coverage a similar goal? How would that change our designs? We might decide that since we have to write a test to cover this error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in thi
error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in this case, making sure that we test all callers and ensure that they obey the meth
However RSpec uses an alternative syntax that reads more like a specification than like a test. Let me show you what I mean.
The fortunate intelligence for people that take place to be superficial for negotiate – and aren’t all of us – could possibly be the simple fact that sixpenny Arizona cardinals NFL jerseys can certainly cheap nfl jerseys be treble attribute. They faculty be sturdy plus they won’t undo easy. They give human being all belonging for that recognizable qualities of substantially overpriced jerseys. The variations in between a tasteless discount women’s nfl jersey and an Football Jerseys high-priced just one lie inside the details and all belonging for that associated bells and whistles.The substantially high-priced youth nfl jerseys are exploit to acquire surplus qualities specified as stitched on lottery with wake less, multicolored artifact layers. mens nfl jerseys They are accomplishment to develop to be fashioned in sizes that mimic the sizes and styles used near to the theater plus they are accomplishment to human being all belonging for that small markings that leave piddle it relaxation out.All of <a href=”http://www.clothing-trading.com/”>Discount NFL Jerseysose components are major, but they are also essentially spare. should you essential to represent your lover squad or participant with affordable youth nfl jerseys online, you can perform so without any all of people design elements.
Catching and rethrowing a different exception is slightly smelly and the code would be cleaner without the try/catch block.
GHD australia have fairly very rated for rather a few of elements just like pattern durability and ease of use.
error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in this case, making sure that we test all callers and ensure that they obey the meth
We are the professional scarfs manufacturer, scarfs supplier, scarfs factory, custom scarfs.
better overall design, in this case, making sure that we test all callers and ensure that they obey the meth
TopCombine Follow ipad bag the detail tips below, you can increase the laptop battery life of a year or more. Game Controllers first thing you should care about the USB Gadgets END!999999999999999999999999999999999999999
If psychologically prepared, all preparations have been completed!
The tests are the specification and documentation of the code, so if something nontrivial but unexpected happens
I love this one. A very good post. Thank you. roof sarasota
I am not sure where you are getting your information, but great topic
very good code. Thanks
Okey oynamak hiç bu kadar zevkli olmadi. Online ve 3 boyutlu okey oyunu oyna ve turnuvalara sende katil.
Should you strive for 100% code coverage from your unit tests? It’s probably not mandatory and if your goal is 100% coverage, you’ll focus on that goal and not focus on writing the best tests for the behavior of your code.?
Should you strive for 100% code coverage from your unit tests? It’s probably not mandatory and if your goal is 100% coverage, you’ll focus on that goal and not focus on writing the best tests for the behaviorMBT Walking Shoes Sale of your code.?
I really love your post Thank you :-)
Have the christian louboutin patent leather pumps is a happy thing. Here have the most complete kinds of christian louboutin leather platform pumps.
We have many style beats by dr dre headphones for you to pick up,what you have to do is choose the one you like most. By our professional service, you can buy your favorite monster beats dr dre headphones from us with full confidence.
We have many style beats by dr dre headphones for you to pick up,what you have to do is choose the one you like most. By our professional service, you can buy your favorite monster beats dr dre headphones from us with full confidence.
Hello Friend,Whichever style of Fashion Shoes you’re looking for, classical, fashionable, lovely or the latest design, you can find your favorite designer shoes in www.dunkpage.com ,several days ago I bought one pair of shoes from there,It’s beautiful and very comfortable!
of Fashion Shoes you’re looking for, classical, fashionable, lovely or beats by dre sale cheap beats by dre
Good Luck!It is nice of you to post it. Designer cheap Men Evisu shorts from China at on line store
hmm ,i’m not sure if this is what i’m looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff
Blog posts about wedding and bridal are always rare to find , at least with great quality,you qualify for a great blog post writer title,kep the great job happening
I have really admired for the nice info is Director of Operations Job Description visible in this blog that to amazing technology Accounting Job Description is visible in this blog Graphic Designer Job Description This is an interesting articles in this blog that to Legal Assistant Job Description using the simple language in this blog
The architecture profession has lost a lot of its integrity, especially in the USA. The general architect here has no scruples, no ambitions.
I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you
well. you guys really give us the sample of C++ programing skill. So. why not try this method and do a better code. next time. have another try.
This post was really nicely composed, and it also contains many helpful details.
100% Code Coverage? 32 hoo,good article!!I like the post!22
100% Code Coverage? 33 good post167
Walking at the mall, one would find different kinds of bags?
http://www.outfitscosplay.com/cosplay-accessories/nana-ai-yazawa-accessories Deluxe NANA Ai Yazawa Accessories for Sale.Find your favorite characters and cosplay outfits from all the popular anime and games.