Search This Blog

Wednesday, July 14, 2010

Netbeans 6.9 - Adding Basic Code Coverage

Netbeans 6.9 - Adding Basic Code Coverage

Description

Netbeans provides (and has done so for quite some time) good JUnit test functions allowing you to with the click of a button generate unit tests. Due to the ease of using and generating unit tests it has encouraged me to use them and so Ive been adding them into my recent code. However, not long after adopting Unit tests on a broad scale I find myself wanting to know how well I'm doing with the use of tests. I need to have some sort of code coverage information. There are at least two main contenders in this area (in the Open Source market of-course) and these are:

These are both aimed at slightly different areas but both provide code coverage info. However, before going ahead and implementing these within Netbeans (and especially Im interested in Cobertura) I decided to use Netbeans 6.9 inbuilt plugin for code coverage (which is based on EMMA anyway).


Implementation of Code Coverage

It was very simple and what follows are the steps to get it to work:
  1. Go to the plugin manager in Netbeans and download and install the Code Coverage tool.
  2. Run your tests across the whole project.
  3. Right click on the project, go to the new 'Coverage' link and 'Activate Coverage Collection' which will allow your code to be instrumented.
  4. Re-run your tests.
  5. Then go to the 'Coverage' and 'Show Project Coverage Statistics'. These will only work after the code has been instrumented and the tests re-run with the instrumentation in place.

This will then give you a report which will show the code coverage across your project for each class and also for the lines within the class and how many are covered by tests (i.e.: it'll look at the branch points like any coverage tool and check you have tests for each case). Furthermore it'll also highlight in your code, when you activate the coverage collection, highlighting which will paint covered code in light green and non-covered in light red so that at a glance you can see what is and isn't covered by tests in your code. Personally I switch this on, run the project stats, view them, see what classes are poorly covered, and then use the colour highlighting to see where I need more tests. However I don't leave it on all the time as I don't like my code painted red/green all the time.

Example Screenshot

The following is a screenshot of the code coverage project stats page along with how to activate it. The numbers indicate:

  1. The new 'coverage' link which appears as part of the plugin install.
  2. Shows where to start gathering stats (by getting the code to be instrumented) and where to trigger the project stats page to be displayed.
  3. The project stats page itself and some of the stats it displays showing class coverage, individual line level coverage etc.

Final Overview

In a future post Id like to look into implementing / using Cobertura in Netbeans and viewing the reports that Cobertura generates through Netbeans in some simple way, although this tool/plugin for viewing code coverage will be quite ample for the time being as it provides me with the ability to see code coverage across the project, the class level and even down to the line level through the coloured highlighting it provides.  This is enough to show me were I need to test more at this stage, with the future updates I think being more a fancy-shmancy kinda thing.

No comments:

Post a Comment