«« I Hit the Pin? Coverage Isn't a Silver Bullet »»
blog header image
Code Coverage

Think that just because you use JUnit and test-driven development that your code is well tested? Actually, probably not.

One of the key tenets of XP is that no line should be in your code that isn't necessary. If you take a line out of your code it should break at least one test. If it doesn't, delete it or write more tests!

So you could spend your time deleting lines and then running the test suite to see if it breaks ... but who wants to do that? That's where a code coverage tool like jcoverage comes in.

Coverage tools work by marking the compiled code so that lines can be counted by the number of times they are executed. Then you can run your test suite over this modified version of your compiled code to see where the test suite hits and where it misses (see picture). You can even see which specific lines of code you forgot to cover.

Then you have a choice: write the tests that cover those lines or decide they are useless and remove them. Some lines are impossible to get to -- a silly example would be:

if (x < 10) {
   y += 1;
   if (100 < x) {
      System.out.println("Am I getting here?");
   }
}

You would never get to the println(). When you refactor complicated code over and over it's hard to tell when you've cut a chunk of code off and made it impossible to execute. That cruft can be removed to simplify what's left.

So your tests test your code and coverage tools examine your tests. It's a great way to keep on top of the quality of your tests and your code. So get a code coverage tool! If you have the means I highly recommend picking one up ... it is so choice.

Posted at June 23, 2003 at 01:04 AM EST
Last updated June 23, 2003 at 01:04 AM EST
Comments

My favourite part is how you can look at the source and it tells you EXACTLY how many times that line was hit. Much better than a simple percentage given.

I don't know about the colours though... I would rather get something in teal and tope.

hahahaha.... ;-)

» Posted by: Jim at June 23, 2003 04:04 PM

Can you use these with VB? :-P lol

» Posted by: roy at June 24, 2003 02:09 AM

Hey, Ryan--

Just googled across your blog. You seem to be familiar with some of the intimacies of JUNIT / JCOVERAGE / ECLIPSE / ANT.

I'm a loyal JUNIT user for Eclipse, and want to start checking how well these JUnit tests hit my code. So that would be, presumably, something JCoverage can do. Which means, in turn, that I need to set up JUnit as a compilation target, and I'm having a lot of trouble with that. Silly tag doesn't seem to want to work.

Can I ask if you've pulled it off?

» Posted by: Dan at July 2, 2003 04:04 AM

I am sort of familiar -- I got it to work in two cases. :) I am going to write a post on how to do this soon, since it's not described well in the Ant manual. I had to piece it together myself from various places.

The jcoverage documentation is also fairly brief and I'd like to do a post on that as well. I'll email you when I get it done so you know ...

» Posted by: Ryan at July 2, 2003 02:14 PM
Google
 
Search scope: Web ryanlowe.ca