Tuesday 14 September 2010

Code coverage - when is it sufficient?

We write unit test cases to ascertain functionality delivered by parts of code. We call this white box testing. But it happens for some part of code unit test cases are missing or sometimes all the paths are not thoroughly tested. Code coverage analysis provides overall and in detail statistics about the current coverage of unit testing.
This kind of statistics is important to find out gap areas and add more test cases to improve overall quality.

How is it measured?

Code coverage tools usually provides overall and in-depth code coverage details by package, class, method and line of Java classes. The reporting tools also have the ability to highlight lines of code covered and not-covered by the units tests. These tools collect data from runtime after test executions to produce such reports.
 
OVERALL COVERAGE SUMMARY:
 
[class, %]       [method, %]      [block, %]          [line, %]            [name]
98% (121/123)!   64% (312/489)!   81% (15500/19172)   77% (2633.6/3441)!   all classes
 
OVERALL STATS SUMMARY:
 
total packages: 1
total classes:  123
total methods:  489
total executable files: 31
total executable lines: 3441
 
COVERAGE BREAKDOWN BY PACKAGE:
 
[class, %]      [method, %]     [block, %]          [line, %]         [name]
98% (121/123)!  64% (312/489)!  81% (15500/19172)   77% (2633.6/3441)! default package

Ref:Emma quickstart 

From the code coverage reports, it is very easy to pin-point classes, methods and lines not covered by the unit tests. With a well integrated CI tool like Hudson,it case the code coverage falls below certain benchmark - it is possible to mark the build as failed.
But at times it is easy to fool around with the code coverage tools with dummy test cases which may show high coverage percentage but actual quality of test cases might not just be sufficient. We will try to find out how much is sufficient code coverage.

How much of code coverage is sufficient?

From the code coverage reports, we see certain percentage data for each class, method and package varying from 64 to 98%. But when do we say, it's sufficient code coverage? is it 64, or 80 or 100%? Is 100 % test coverage even sufficient?
The trick is - even 50% test coverage is sufficient if the test cases are well written while 100% coverage might also mean wrong data:
Code Test Case Notes
public int add(int x, int y){
  return x+y;
}
@Test
public add(){
  add(4,5);
  assertTrue(true);
}
While the coverage would be 100% but not sufficient.

Ways to ensure sufficient code coverage

I believe ensuring good test coverage thus good quality is more in the mindset of developers - which for a large group is achieved by well-defined process and practice. Here are my tips for good code coverage :
  • Follow TDD : Ensure you write a test case before writing your code. If you find this difficult, probably you have not understood the business functionality well. Never ever code or fix an issue without a proper test case.
  • Peer Review : Who is your greatest criticizer? Find him (or her) and get your code reviewed. First few rounds of harshness would shape your test cases and code well.
  • Run Code Coverage Tools Often : Don;t keep the code coverage review reports for the end - run often and run for small part (each class). Fixing it early when the memory is fresh helps to add quality test cases
  • Remove unused code : Feel free to delete unwanted code not covered by test cases.


References