Sunday, January 6, 2008

Cobertura - A tool to help you measure your Java unit test code coverage

If you've ever written unit test cases for any program language, you know how important it is to ensure that your test cases execute each line of code. This is called code coverage. The more your unit test cases exercise the logic in your application code, the less likely it is that a bug slips through your unit test cases. On a large code base (common to most applications these days), it's hard to judge how effectively your coverage statistics. There are various tools to help quantitatively measure the coverage of your unit test cases. Such vendor tools that accomplish this are Parasoft's JTest, and Quest's JProbe. But who wants to buy software? I've been trying out this open source code coverage measurement tool called Cobertura. So far I've only been using the ant tasks in combination with HTML reports. The reports it produces are really nice and informative, even allowing you to drill down into each class such that you can view the source and actually see the number of times each line of code was called from your test suite. Alternatively, red highlighting will call those lines with no coverage to your attention. There's also a command line interface if you choose to execute it outside of your build. Check it out here:
http://cobertura.sourceforge.net/
Hopefully I get the initiative to post some examples of how to include it in your ant targets.

Other tools that have my attention at the moment, and could very possibly surface in a future blog post include:

No comments: