TestNG with Cobertura – generate code coverage report

Cobertura is a open source coverage tool which calculates the percentage of code accessed by test and generate source code coverage report.

Prerequisites

JDK 1.5 or higher, TestNG and Cobertura.
You can download Cobertura from http://cobertura.sourceforge.net/download.html

How to Integrate Cobertura and TestNG to generate code coverage report?

In order to integrate Cobertura into our build process, we need to add the cobertura library to classpath and import the tasks provided by the tool.

<taskdef classpath="${app.classpath}" resource="tasks.properties" />

Then create an Ant tasks to instrument the compiled classes.

<target name="cobertura" depends="compile">
  <cobertura-instrument todir="${coverage-output}">
    <fileset dir="${${build.classes.dir}}">
      <include name="*/**.class" />
    </fileset>
  </cobertura-instrument>
</target>

Once we have generated the instrumented classes, we can run the tests against them, from below example, it will run all TestNG cases and produces a cobertura.ser file. cobertura.ser is a date file for storing the metadata about your classes including method name, line numbers, etc. Note that if the file already exists, the new coverage information will be merged into it.

<target name="coverage-test" depends="cobertura">
  <testng classpathref="app.classpath">
  <classfileset dir="${coverage-output}" includes="**/*.class"/>
  </testng>
</target>

Finally, after having coverage ser data, all that remains is to run a report to view it:

<target name="cobertura.reports">
  <cobertura-report format="html" srcdir="${build.src.dir}" destdir="${reports}"/>
</target>

The entire Ant build XML:

<?xml version="1.0"?>
<project name="TestNG and Cobertura - Generate code coverage report" 
default="cobertura.reports" basedir=".">
	<tstamp />

	<property name="build.src.dir" value="src" />
	<property name="build.lib.dir" value="lib" />
	<property name="cobertura.dir" value="${build.lib.dir}/cobertura" />
	<property name="testng.dir" value="${build.lib.dir}/testng" />
	<property name="build.classes.dir" value="build/classes" />
	<property name="coverage-output" value="coverage-output" />
	<property name="reports" value="reports" />

	<path id="app.classpath">
		<fileset dir="${testng.dir}">
			<include name="Testng-5.14.1.jar" />
			<include name="lib/**/*.jar" />
		</fileset>

		<fileset dir="${cobertura.dir}">
			<include name="cobertura.jar" />
			<include name="lib/**/*.jar" />
		</fileset>
	</path>

	<taskdef classpath="${app.classpath}" resource="tasks.properties" />

	<target name="compile" description="Compile sources" depends="clean">
		<echo message="compile classes from ${build.src.dir}" />
		<mkdir dir="${build.classes.dir}" />

		<javac destdir="${build.classes.dir}" debug="${javac.debug}" 
optimize="${javac.optimize}" deprecation="${javac.deprecate}" verbose="${javac.verbose}">
			<src path="${build.src.dir}" />
			<include name="**/**.java" />
			<classpath refid="app.classpath" />
		</javac>
	</target>

	<target name="cobertura" depends="compile">
		<cobertura-instrument todir="${coverage-output}">
			<fileset dir="${build.classes.dir}">
				<include name="*/**.class" />
			</fileset>
		</cobertura-instrument>
	</target>

	<target name="coverage-test" depends="cobertura">
		<testng classpathref="app.classpath">
			<classfileset dir="${coverage-output}" includes="**/*.class" />
		</testng>
	</target>

	<target name="cobertura.reports">
		<cobertura-report format="html" srcdir="${build.src.dir}" destdir="${reports}" />
	</target>

	<target name="clean">
		<echo message="clean classes and jar files" />
		<delete dir="${build.classes.dir}" />
		<delete dir="${build.output.dir}" />
		<delete file="${coverage-output}" />
	</target>

</project>

output:

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

*

code

  1. Not Relevant

    See also ReportNG ( http://reportng.uncommons.org/ )

    Ответить
  2. akshat

    Can you explaing what is the below line ment for :

    I am unsig TestNG for my testing few JAR libraries. I am using ANT for build my test project. But using your ANT build XML abive it is ont working for find the code coverage using Cobertura.

    Can you please help.

    Thanks in advance
    Akshat

    Ответить
    1. admin автор

      Sorry, I don’t find the line you mentioned in the article. something missed here?

      Ответить