Using Cobertura in unix to generate the code coverage reports - java

I m using cobertura-1.9.4.1 to generate code coverage reports.First I set the classpath to cobertura.jar and to other jars in the lib folder. Then I execute cobertura-instrument.sh.
But on executing I get the error loaded information on 0 classes . I m giving the complete path to the compiled classes still it is unable to instrument the classes .
So, what am I missing or what could be the possible reasons for this.

Do you mean the error is during the instrumentation, or that after running your tests, the coverage still shows zero?
Here's an example of instrumentation (with Ant):
<target name="--coverage.instrument">
<delete file="cobertura.ser"/>
<mkdir dir="${coverage.instrumented.dir}"/>
<cobertura-instrument todir="${coverage.instrumented.dir}">
<fileset dir="${classes.main.dir}">
<include name="**/*.class"/>
<exclude name="**/*Test.class"/>
</fileset>
</cobertura-instrument>
</target>
Don't forget that you need this sysproperty when testing (eg in Ant Junit task):
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
Once Cobertura is set up an instrumentation has happened, an example of execution:
<target name="--test.unit">
<mkdir dir="${temp.dir}/unit-tests"/>
<junit forkmode="perBatch" printsummary="yes" haltonfailure="no" haltonerror="no"
failureproperty="unit.tests.failed">
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
<classpath refid="classpath.test.utest"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${temp.dir}/unit-tests">
<fileset dir="${java.src.utest.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>

I believe that recent versions of Cobertura don't work well with JDK5. Strongly suggest upgrading the JDK.

Related

Ant JUnit Batchtest .java or .class

I have seen a few links which appears to imply that JUnit tests can be executed from the .java file instead of the .class
For example
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
Instead of
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
Is the first example a valid case? I could not get it working due to ClassNotFoundExceptions
I agree with the comment above, that it looks like the tag allows for either .java or .class.
I did a small test and when I ran a test with this setup - where the dir=somefolder, then using <include name="**/*Test.java"/> when the folder pointed at contains class files, then Ant will basically have an empty fileset to process for *Test.java, but when using <include name="**/*Test.class"/> then the fileset was not empty and the test cases will get run.
This was the result of my quick test. As far as I could tell, it looks like you need to specify the *Test.class to pickup the test cases.

Why does Cobertura seem to switch my Eclipse's Java version?

These are the steps that create the error:
Run my unit tests within Eclipse (Cntrl-F11). All 67 succeed and report as such.
Using an Ant build.xml file, run a Cobertura task to generate a coverage report. The task fails stating that a unit test has failed.
When I try to run the unit tests using Eclipse again, I receive the following error:
Unsupported major.minor version 51.0
(These symptoms persist until I use Project -> Clean within Eclipse.)
My Cobertura ant task is:
<target name="report-test" description="Generate a test coverage report." depends="clean, compile">
<taskdef resource="tasks.properties">
<classpath>
<pathelement location="${lib.dir}/cobertura-1.9.4.1/cobertura.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/asm-3.0.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/asm-tree-3.0.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/log4j-1.2.9.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/jakarta-oro-2.0.8.jar" />
</classpath>
</taskdef>
<cobertura-instrument todir="${build.dir}/cobertura-instrument">
<fileset dir="${build.dir}">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
<junit printsummary="yes" fork="true" haltonfailure="yes" showoutput="yes">
<classpath location="${build.dir}/cobertura-instrument"/>
<classpath location="${build.dir}"/>
<classpath refid="classpath.test" />
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
<formatter type="xml" />
<batchtest todir="doc/junit">
<fileset dir="${test.dir}" />
</batchtest>
</junit>
<cobertura-report srcdir="${src.dir}" destdir="doc/coverage" format="xml" />
<delete file="cobertura.ser"/>
</target>
Coberatura hasn't switched Eclipse's Java version.
What it has actually done is recompile the classes using a version of Java that is more recent than the one you are using to run Eclipse. Eclipse can't load those .class files.
The short term solution is to get Eclipse to clean and rebuild the project(s) after running Coberatura ... as you are currently doing.
In the long term, you should either change Eclipse to run using the same Java version as your Ant builds, or change the Ant build file so that it doesn't write the ".class" files into the Eclipse workspace. Or both ... 'cos having something else writing stuff into the Eclipse workspace is going to cause other problems too.

How do you execute a subset of JUnit tests using Ant?

We have a large project and executing all our unit tests takes a lot of time. We are also using Ant. What strategies could be used to execute only a subset of our tests using Ant? We have our tests in groups of logical java packages, so that could be of help...
You should use batchtest tag of the junit ant task. Check here : http://ant.apache.org/manual/Tasks/junit.html
A simplified example from that link is given below:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<formatter type="plain"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>

Unit test fails only when running from with Ant task

I run the same tests from both eclipse and from an Ant task. When running from eclipse all tests pass. When I run Ant junit task, one, single test fails with the following strange error:
junit.framework.AssertionFailedError
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)
at unitests.mypackage.MyTestClass.myTestCase(Unknown Source)
What can be the cause?
I read a little and found out that it might be because eclipse and Ant use different versions of junit. In my project, junit is located at libs/junit-4.10.jar and referenced both in eclipse's .classpath file and in junit task classpath.
You can see Ant's task here:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${src.dir}" includes="**/*.jar"/>
</path>
...
<target name="run-unit-tests" depends="compile,compile-unit-tests">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="yes" haltonfailure="no">
<classpath>
<path refid="classpath"/>
<fileset dir="${unit.tests.classes.dir}" includes="**/*.class"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${junit.output.dir}">
<fileset dir="${unit.tests.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
<mkdir dir="${junit.report.dir}"/>
<junitreport todir="${junit.report.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junit.report.dir}/html"/>
</junitreport>
</target>
The version of Ant is 1.7.1 and it came with eclipse.
Edit:
Eventually solved it by adding fork="yes" to junit's task. Found it by generating build file using eclipse's export option, and then looking at the differences between the generated file and mine. No idea why forking solves the problem though.
Different versions of Ant sound like a likely cause. To test that, set which Ant version you want Eclipse to use.
It's probably also a good idea to set the same version of JUnit and the same JDK that you use to run the test outside the IDE.
As the stack trace shows org.eclipse entries, I strongly suspect you are running ant via Eclipse. If you run ant from the console you will very likely not have this problem.
The cause, therefore, is a bug in Eclipse. This should be no surprise, Eclipse is full of bugs.
fork="true" helps because it causes ant to spawn a new process for execution. This new process does not have any Eclipse classes on the classpath.

Run all unit tests with Ant builder

I have a directory with a bunch of JUnit tests in my project. So far I have used separate target for each unit test. For example:
<target name="MyTest">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="tests.MyTest" todir="${junit.output.dir}"/>
<classpath refid="MyProject.classpath"/>
</junit>
</target>
This method requires me to change build file every time I add a Unit test.
I want to able able to to run all unit tests in the project with a single Ant builder target. Is it possible to do?
Yep it is, you need to look at the fileset tag, e.g:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${MyProject.classpath}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
The important part is the use of fileset and a glob/wildcard pattern to match the names of the tests. Full docs on the junit task with examples here:
http://ant.apache.org/manual/Tasks/junit.html
Yep! We do it using an ant command batchtest. Looks like this:
<batchtest todir="${junit.report.dir}">
<fileset dir="${basedir}\test\unit">
<include name="**/*Test.java" />
</fileset>
</batchtest>
Google it, it should sort you out

Categories