I have mentioned Cobertura in this blog.
Cobertura is a tool similar to Coverlipse mentioned in this blog. Cobertura is a measurement tool to measure your unit tests’ coverage on your tested codes. I think that it is really a good tool. It is a bit difficult for a beginner, because it requires some configuration. You have to know how to use Ant (a build tool). Here, I just provide a tutorial to get a quick overview of the Cobertura on Windows.
1. You need to have Ant installed on your computer. You can download from this page. Select apache-ant-1.7.1-bin.zip. See this page for installing instruction. Be sure to add Ant to your environmental variable called PATH as illustrated in “Windows / OS 2″ section in the installing instruction page, so you can call Ant from any folders. After you install ant, try running just command “ant” in any folder in your command promt. If it displays something like Ant is not recognized as a command, it means that you have not set up the environment variable PATH correctly. See this page for instruction on how to set an envrionmental variable on Windows.
2. download the Cobertura from this page. Select cobertura-1.9-bin.zip.
3. Unzip into your folders. You will get cobertura-1.9 folder
4. Put your Java source files into the example folder. There is an example found in \cobertura-1.9\examples\basic. I put my Java source files which have been used as an example in this blog and this blog. It consists of one Java file, DoSomething.java, and its test case file, TestCountCharacter.java. Basically, put your codes in \cobertura-1.9\examples\basic\src folder.

Put your java files (unit test case and class to be tested) in the cobertura-1.9examplesasicsrc folder
5. Put JUnit.jar into \cobertura-1.9\lib.
See this blog for how to get JUnit.jar.
If you do not put this Jar file here, then build will be failed, because it can not compile some classes which use come classes found in JUnit.jar. The error is shown below. If your Java compiler can find JUnit.jar, then you do not need to add JUnit.jar into the lib folder.
6. Modify the ant build file found in \cobertura-1.9\examples\basic\build.xml.
On the line around 90, add your java test file into it. In my case, I have to add TestCountCharacter.java which contains the JUnit test cases, so I put “<include name=”TestCountCharacter.java” />” as shown below.
<batchtest todir=”${reports.xml.dir}” unless=”testcase”>
<fileset dir=”${src.dir}”>
<include name=”**/*Test.java” />
<include name=”TestCountCharacter.java” />
</fileset>
</batchtest>
7. Run the ant build file simply by typing “ant” in the \cobertura-1.9\examples\basic.
Behind the scene, Cobertura compiles, run test cases and produces the report on your test case coverage. See this page for full instruction.
8. View the report. Reports are automatically generated after the ant build. There are two reports: report on JUnit test case and report on test coverage.
You can view the report of your JUnit test case by opening up the index file in cobertura-1.9\examples\basic\reports\junit-html.
You can view the report of your test coverage by openning up the index file in cobertura-1.9\examples\basic\reports\cobertura-html.
As you see from the figure below, the line coverage is not 100% achieved because there some statements which have not been executed. The branch statement does not achieve 100% coverage, because there is a branch which has not been executed.
If you furhter click into the class DoSomething.java, it shows you which line has not been executed and how many time that line has been executed as shown below.






Hi,
I just checked your Blog and found this interesting.Since I was looking a kind of tool which provides efficient testing.
Anyway I tried but I have some doubts when I edited “build.xml” file. There is an error such as
> BUILD FAILED : invalid byte 1 of 1-byte UTF-8 sequence
thanks,
Comment by Ali Sura — August 16, 2008 @ 6:29 pm
Hello Ali,
Try extracting the cobertura-1.9-bin.zip again and start afresh again.
Comment by schaiyakul — August 16, 2008 @ 7:06 pm