Saturday, October 23, 2010

Identifying Untested Code with Cobertura

With the No Fluff Just Stuff code base, I'm currently at 87% code coverage. Life at this level of coverage is pretty good. I'm rarely surprised by bugs as a result of untested code. Most untested code is pretty inconsequential stuff; such as, legacy (pre-groovy era code) that was originally written only to handle checked exceptions.

That said.. I'm always trying to increase coverage. My code base is just shy of 20,000 lines of code. To increase coverage by %1, I have to add 200 lines of coverage. Uncovered lines are sprinkled throughout the codebase and rarely in blocks larger than 1-3 lines. Writing a unit test to cover 1-3 lines typically involves writing 5-10+ lines of unit test code. So at this point, a %1 increase in test coverage is a lot of work and is something to celebrate.

The html reports generated by cobertura are great, but I find that they don't point me directly to the class with the most untested lines of code. So using the xml report, I wrote a simple gant (groovy) target to generate a report which orders classes by lines of uncovered code.

It is a simple script that took me about 30 minutes to write, but should help me attack my uncovered code a little more efficiently.

This script is on github: http://gist.github.com/642533. clone url: git://gist.github.com/642533.git

Sample output:

2 comments:

  1. Ben, this is awesome. A run of Sonar against the codebase should also yield some "hotspots" that have good ROI.

    ReplyDelete
  2. Thanks... I've been hearing about it and need to check it out!

    ReplyDelete