Google

Thursday, February 17, 2005

Introduction on JCoverage

Hi!

Today, I attended a small session on JCoverage. Not much in detail. But enuf to move forward. JCoverage lets you find out how much of your code is covered with your JUnit test test cases.

In the example we worked on, we wrote some code and Junit test cases.
Then we modified our ant build file. We defined another target 'instrument' like below:

<target unless="skip.build.instrument" depends="init" name="instrument">
<taskdef resource="tasks.properties" classpath="jcoverage/jcoverage.jar">
<instrument todir="${instrumented.dir}"">
<fileset dir="${classes.dir}"">
<include name="**/*.class"">
<exclude name="**/*Test.class"">
</fileset">
</instrument">
</target">

In this target, we take all our source class files. JCoverage takes these class files and then creates new class files, which are almost identical to the original class files, with the exception that after every line of code, it puts some line of code, which will help it to find out later whether the line of code was unit tested or not.

After this we modify our 'junit' target like below

-<target unless="skip.build.junit" depends="init" name="junit">
-<junit dir="${basedir}" fork="yes" errorproperty="test.failed"
failureproperty="test.failed">
<classpath location="${junit.other.classpath}">
<classpath location="jcoverage/jcoverage.jar">
-<classpath location="${instrumented.dir}">
<classpath location="${classes.dir}">
<formatter type="xml">
<test name="${testcase}" todir="${result.dir}/reports/junit" if="testcase">
-<batchtest unless="testcase" todir="${result.dir}/reports/junit">
-<fileset dir="${src.dir}">
<include name="**/*Test.java">
</fileset>
</batchtest>
</junit>
<report destdir="${result.dir}/reports/coverage" srcdir="${src.dir}">
<report destdir="${result.dir}/reports/coverage" srcdir="${src.dir}" format="xml">
</target>

As you can see we have modified our class path here. So what happens is that when you execute the test cases, it picks the instrumented class files and not the original class files.

We then defined another task "jcoveragereport' like below

-<target unless="skip.build.jcoveragereport" depends="init" name="jcoveragereport">
<taskdef resource="tasks.properties" classpath="jcoverage/jcoverage.jar">
<report destdir="${result.dir}/reports/coverage" srcdir="${src.dir}">
<report destdir="${result.dir}/reports/coverage" srcdir="${src.dir}" format="xml">
</target>

This will generate some reports. In these reports, it tells us how many lines of code were covered in the test execution. How much percentage of branches were covered in test execution and so on. It also shows the source file in this report, with all the lines which were not covered in the test cases marked in yellow color.

For example: if there is some code in our source file like this

if(...)

......

else
......


Now, if in our test cases, with some input values, we executed the if part of the code block, and didnt check the else part, in the report it will mark the else part in yellow color, indicating that this portion of code was not unit tested.

You can more details on Jcoverage from http://www.jcoverage.com/

Happy learning
Deepak

2 Comments:

Anonymous Anonymous said...

Hi Deepak,

I read ur article. It is good. But I have some corrections. Plz correct me if I am wrong

1. Jcoverage can be used not only for the Junit test cases but also functional test (manually). In case of manual testing you instrument the code and keep the instrumented classes in the class path so that Jcoverage takes the instrumented classes.
2. When the class is instrumented, instrument code is added before and after each line of ur actual code

Thanks,
Manjula Gundluri

1:13 AM  
Blogger Deepak Saini said...

Technically, I think what you say is correct. We can use the instrumented classes and do regression testing on these test classes and we can generate report from that. I dont find any problem with that.

And your second point, that the instrumented code is added before and after each line of code is also correct.

Thanx for your input.:))

Happy learning
Deepak

1:16 AM  

Post a Comment

<< Home