Skip to main content

developerWorks >  Java technology  >  Forums  >  Improve Your Java Code Quality  >  developerWorks

Code coverage of TestNG tests    Point your RSS reader here for a feed of the latest messages in this thread


Tags for this thread: 

     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
Permlink Replies: 1 - Pages: 1 - Last Post: Apr 5, 2006 8:00 PM Last Post By: AndyGlover
AndyGlover

Posts: 69
Registered: Jan 31, 2006 09:46:55 PM
Code coverage of TestNG tests
Posted: Mar 12, 2006 06:15:04 PM
Click to report abuse...   Click to reply to this thread Reply
JUnit's de facto status in the Java community has resulted in coverage tools tailoring their documentation to facilitate coverage via JUnit test cases. TestNG, however, is an exciting framework, which addresses some of the short comings in JUnit with flexible fixtures, parameterized tests, and dependent tests (to name a few).

Using a coverage tool like Cobertura requires a few key modifications to an Ant build file. First, follow the first two steps outlined in the Cobertura documentation (initializing the task and instrumenting the code under test). Next, utilize the TestNG Ant task; however, add the following aspects: (i) include the instrumented code in the TestNG task's [b]classpath[/b] attribute, (ii) create a [b]sysproperty[/b] which points to the binary report file for Cobertura, and (iii) add a [b]classpath[/b] element which includes Cobertura's classpath, which was most likely defined when initializing Cobertura.

For example, a combined coverage-TestNG Ant target would look like this:

code
<target name="testng-cov" depends="testng-init,instrument">
<mkdir dir="${testng.output.dir.comp}"/>

<testng groups="unit,component,system"
outputDir="${testng.output.dir.comp}"
sourceDir="${testng.source.dir}"
classpath="${cov.inst.dir};${testclassesdir};${classesdir}">

<sysproperty key="net.sourceforge.cobertura.datafile"
file="${basedir}/cobertura.ser" />

<classfileset dir="${testng.source.dir}">
<include name="**/*Test.java"/>
</classfileset>

<classpath>
<path refid="cobertura.classpath" />
<path refid="build.classpath"/>
</classpath>
</testng>

<mkdir dir="${cov.report.dir}"/>

<cobertura-report format="html"
datafile="${basedir}/cobertura.ser"
destdir="${cov.report.dir}" srcdir="${src.dir}" />
</target>
[/code]

For more information on TestNG see http://www.testng.org or see Filippo Diotalevi's "TestNG makes Java unit testing a breeze" (http://www-128.ibm.com/developerworks/java/library/j-testng/).
AndyGlover

Posts: 69
Registered: Jan 31, 2006 09:46:55 PM
Re: Code coverage of TestNG tests
Posted: Apr 05, 2006 08:00:08 PM   in response to: AndyGlover in response to: AndyGlover's post
Click to report abuse...   Click to reply to this thread Reply
Justin Lee of Antwerkz (http://www.antwerkz.com) posted on the TestNG users list an example of how to utilize Emma (http://emma.sourceforge.net/) to obtain code coverage via Ant. The Ant code below sets up a web app for testing via jWebUnit (http://jwebunit.sourceforge.net).

code
<path id="test.class.path">
<pathelement location="${emma.instr.dir}"/>
<pathelement location="${build.dir}/test"/>
<pathelement location="${src.xml.dir}"/>
<path refid="project.class.path"/>
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask"
classpathref="test.class.path"/>
<taskdef name="emma" classname="com.vladium.emma.emmaTask"
classpathref="test.class.path"/>
<emma>
<instr instrpath="${war.build.dir}/WEB-INF/classes"
destdir="${emma.instr.dir}"
metadatafile="${emma.dir}/metadata.emma" merge="true"/>
</emma>
<copy todir="${war.build.dir}/WEB-INF/classes">
<fileset dir="${emma.instr.dir}"/>
</copy>
<testng classpathref="test.class.path"
outputDir="${report.build.dir}"
sourcedir="${test.build.dir}" haltOnfailure="false">
<classfileset dir="${src.test.dir}" includes="*/.java"/>
<jvmarg value="-Demma.coverage.out.file=${emma.dir}/coverage.emma"/>
<jvmarg value="-Demma.coverage.out.merge=true"/>
</testng>
<emma>
<report sourcepath="${src.java.dir}">
<fileset dir="${emma.dir}">
<include name="*.emma"/>
</fileset>
<txt outfile="${emma.dir}/coverage.txt"/>
<html outfile="${emma.dir}/coverage.html"/>
</report>
</emma>
[/code]
 Tags
Help

Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular type of content or application that you're viewing.

My tags shows your tags for this particular type of content or application that you're viewing.

 

MoreLess 


Point your RSS reader here for a feed of the latest messages in all forums