Wednesday, February 22, 2012

How to collect Code Coverage for Java Programs?

Byte code Instrumentation is a process of modifying your java byte code so that certain runtime information like code coverage data, profiling data can be collected for later analysis. Using byte code instrumentation we can trace the execution of the running program and monitor the object creations etc. The java memory profiling tools also instrument the byte code for this reason. In this article I’m going to show how we can instrument the byte code and collect the coverage data for a simple java project using Emma code coverage tool and with the help of build tool Ant.

There are two ways to collect the coverage data.
  1. On the fly code coverage: On the fly code coverage is when the program stops executing the coverage reports can be seen. Usually this method is used in automated test suits.  
  2. Offline Code: Coverage Offline coder coverage means that once the program testing is over, separately the coverage report is generated. This gives the flexibility for developers to generate coverage reports at any time.
 Get started with collecting code coverage reports for your java projects.
  1. Download a recent stable version emma jar. You need emma.jar and emma_ant.jar if you are using ant build tool for building your java projects.
  2. Get started with your project by following the instructions by provided by Emma.
How to merge coverage data?

This is important when developers are testing the instrumented Java Application. When each developer is done with their testing, they will be getting coverage.emma file. The coverage.emma files have to be merged before generating the coverage report.The 'report' ant task takes .emma files as input, you need to place all the coverage.emma files into a folder and provide these emma files as input to 'report' task to generate the collective coverage report.

Why is code coverage important?

Code coverage tells how strongly the code written by is tested. The code coverage report gives you confidence on the quality of your test cases. It helps you to understand what portion of your is covered during testing what portion is not covered, so that you can write test cases to cover the uncovered code. For any unit testing and developer integration testing activities, the collecting code reports is recommended.

 I'm explaining this in short here, a detailed article is going to be published on this blog in few days.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...