100% Code Coverage? 36

Posted by Dean Wampler Thu, 17 May 2007 03:50:00 GMT

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

Comments

Leave a response

  1. Avatar
    Jason Bock 7 months later:

    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.

  2. Avatar
    chanel jewelry over 3 years later:

    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

  3. Avatar
    chanel jewelry over 3 years later:

    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

  4. Avatar
    Pandora over 4 years later:

    However RSpec uses an alternative syntax that reads more like a specification than like a test. Let me show you what I mean.

  5. Avatar
    mens nfl jerseys over 4 years later:

    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.

  6. Avatar
    Criminal Records over 4 years later:

    Catching and rethrowing a different exception is slightly smelly and the code would be cleaner without the try/catch block.

  7. Avatar
    ghd australia over 4 years later:

    GHD australia have fairly very rated for rather a few of elements just like pattern durability and ease of use.

  8. Avatar
    Pittsburgh events over 4 years later:

    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

  9. Avatar
    dswehfhh over 4 years later:

    We are the professional scarfs manufacturer, scarfs supplier, scarfs factory, custom scarfs.

  10. Avatar
    clothing manufacturer over 4 years later:

    better overall design, in this case, making sure that we test all callers and ensure that they obey the meth

  11. Avatar
    ipad bag over 4 years later:

    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

  12. Avatar
    Gucci Outlet Online over 4 years later:

    If psychologically prepared, all preparations have been completed!

  13. Avatar
    pandora over 4 years later:

    The tests are the specification and documentation of the code, so if something nontrivial but unexpected happens

  14. Avatar
    Jane jemson over 4 years later:

    I love this one. A very good post. Thank you. roof sarasota

  15. Avatar
    programy over 4 years later:

    I am not sure where you are getting your information, but great topic

  16. Avatar
    okey oyunu oyna over 4 years later:

    very good code. Thanks

    Okey oynamak hiç bu kadar zevkli olmadi. Online ve 3 boyutlu okey oyunu oyna ve turnuvalara sende katil.

  17. Avatar
    MBT Shoes UK Sale over 4 years later:

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

  18. Avatar
    mbt walking shoes sale over 4 years later:

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

  19. Avatar
    real estate advertising over 4 years later:

    I really love your post Thank you :-)

  20. Avatar
    christian louboutin shoes on sale over 4 years later:

    Have the christian louboutin patent leather pumps is a happy thing. Here have the most complete kinds of christian louboutin leather platform pumps.

  21. Avatar
    beats by dr dre headphones over 4 years later:

    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.

  22. Avatar
    beats by dr dre headphones over 4 years later:

    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.

  23. Avatar
    Hancy over 4 years later:

    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!

  24. Avatar
    beats by dre store over 4 years later:

    of Fashion Shoes you’re looking for, classical, fashionable, lovely or beats by dre sale cheap beats by dre

  25. Avatar
    bagsupplyer over 4 years later:

    Good Luck!It is nice of you to post it. Designer cheap Men Evisu shorts from China at on line store

  26. Avatar
    Diablo3 over 4 years later:

    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

  27. Avatar
    DR OZ african Mango over 4 years later:

    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

  28. Avatar
    shanewatson404@gmail.com over 4 years later:

    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

  29. Avatar
    Tips For Bowling over 4 years later:

    The architecture profession has lost a lot of its integrity, especially in the USA. The general architect here has no scruples, no ambitions.

  30. Avatar
    alwadifa over 4 years later:

    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

  31. Avatar
    iPhone sms transfer over 5 years later:

    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.

  32. Avatar
    krzyzowki edukacyjne over 5 years later:

    This post was really nicely composed, and it also contains many helpful details.

  33. Avatar
    louboutin sales over 5 years later:

    100% Code Coverage? 32 hoo,good article!!I like the post!22

  34. Avatar
    bladeless fans over 5 years later:

    100% Code Coverage? 33 good post167

  35. Avatar
    mini plime hermès over 5 years later:

    Walking at the mall, one would find different kinds of bags?

  36. Avatar
    cosplay outfit over 5 years later:

    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.

Comments