Ubuntu insights, Programming in groovy, java, et als!

Sunday, December 18, 2011

Eclipse : Configuring eCobetura for Code Coverage

Recently, I stumbled across cobertura while searching for a decent code coverage utility for my java and groovy units. Major parts of Cobertura are under the GNU public license which I found would suffice my need for basic code coverage. You can read more about cobertura here at http://cobertura.sourceforge.net/

eCobetura is a comfy eclipse plugin that generates a code coverage report within the eclipse console itself..Here is how you can configure it.

Eclipse update site for eCobertura

http://ecobertura.johoop.de/update/

In eclipse, Under Help menu -> Install New software, add up the above (url) update site..

Check eCoberture code coverage option and proceed with Next until all the dependencies get installed..

Restart eclipse after installing the eCobertura plugin..

Sample unit test code coverage using eCobertura
Create a sample Java/Groovy project.

Create a java/groovy class PrintUtil

public class PrintUtil {                        
public String printHello(String msg){      
return "Hello "+msg;                    
}                                          

public String printGoodday(String msg){    
return "Good day "+msg;                 
}                                          
}                                               



Likewise create a JUnit/Gunit testcase class that will test the functionality of PrintUtil class.

import junit.framework.TestCase;                
public class PrintUtilTest extends TestCase {   
PrintUtil util = new PrintUtil();           
public void testPrintHello(){               
String out = util.printHello("World");  
assert(out=="Hello World");             
}                                           

public void testPrintGoodday(){             
String out = util.printGoodday("World");
assert(out=="Hello World");             
}                                           
}                                               

In eclipse, Under Window menu -> Show View -> Other, Select the Coverage Session View option to view the eclipse console for the code coverage results of the above unit test..

Select the PrintUtilTest testcase in the eclipse package explorer, Find the new shorcut for eCobertura in the eclipse toolbar (probably just best the debug menu icon).. Execute the PrintUtilTest as a junit under the Coverage As -> JUnit Test option.. The complete code coverage details will be shown in the Coverage Session View similar to the one shown below..


PS: This post focuses only about eCobertura, an eclipse plugin for Cobertura which can be used in tandom while running junits in eclipse.. However the original, Cobertura has more advanced features to generate a complete XML/ HTML reports on code coverage, etc..It can be run from command line or as an ant or Maven build.. You might want to exploit it for advanced reporting options.. 




1 comments:

Diego said...

interesting post , code coverage is a good practice for our unit tests.

Post a Comment