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: