Configuring ReportNG with ANT build.xml

In previous tutorial, we have seen configuring ReportNG with TestNG testng.xml and Now we will see configuring reportNG with Ant build.xml.

TestNG allows to define different ant testng tasks in build.xml file. The below are the few configuration options that can be used along with testng tasks.

1. Listeners - We can add Comma or space separated list of fully qualified class names that implements TestNG iTestListener or iReported as a listener to testng task.

2. useDefaultListeners - Whether the default listeners and the reporters should be used. Need to pass a Boolean value.

3. Methods - A comma separated list of fully qualified class name and method can be passed. Example: 'com.pack.sample.test1'.

4. outputdir - The directory where the report is generated

5. Parallel : Parallel mode to use for running tests or classes or methods.

6. threadCount - The number of threads that can be used for this run.

7. dataProviderThreadCount - The number of threads to use for data providers for this run.

There are many other configuration options available for TestNG Ant. You can refer TestNG ANT website.

Now we have to set the listeners attribute of the TestNG element in build.xml file. We can pass multiple attributes with comma separated values. And add the below two listeners to the build.xml file as we used in testng.xml file.

org.uncommons.reportng.HTMLReporter
org.uncommons.reportng.JUnitXMLReporter

Before executing make sure you have added reportng.xv.x.jar and velocity-x.x.jar. When using with TestNG we may encounter an issue as below

   [testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Injector 

Many people have found this issue and able to solve this by downloading guice.x.jar and adding the Guice JAR(s) to the classpath when TestNG/ReportNG runs

After adding all the jar files to the library folder, the project structure should look like below:
Ant project structure

The below is the Ant task which accepts the below attributes :

classpathref : - A reference to a PATH-like structure for the tests to be run.
haltonfailure : - Stop the build process if a failure has occurred during the test run.
listeners : - A comma or space-separated list of fully qualified classes that are TestNG listeners (org.uncommons.reportng.HTMLReporter and
org.uncommons.reportng.JUnitXMLReporter)
outputdir : - Directory for reports output where you want to save the reports

You can find about more attributes and its uses in 'TestNG Ant Document'

<!-- Runs the file and generates Reportng report for TestNG-->
<target name="testng-execution" depends="compile">
		<mkdir dir="${testng-report-dir}" />
		<testng outputdir="${testng-report-dir}" haltOnfailure="true" classpathref="classpath" useDefaultListeners="true" 

listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter">
			<xmlfileset dir="${basedir}" includes="testng.xml" />
		</testng>
	</target>

NOTE: You can find the complete build.xml in the previous example. If you have defined your own, please just add the above task to your build.xml.

After adding the above code to the build.xml file and executing it we should the report. And in the above code we have set 'useDefaultListeners="true"' which will also generate testNG report along with the reportNG report.

We can also disable default TestNG report by setting 'useDefaultListeners="false"'. Check the below image to check the difference in the project structure.

Ant project structure

Below is the report that is generated after executing build.xml file.

Ant project structure

There is an other element that we have can use in the above TestNG task to over-ride the default report title. below is the syntax that we can use to over-ride the default reportNG report.

<sysproperty key="org.uncommons.reportng.coverage-report" value="My Test Project Report"/>

In the next tutorials, we will see more such properties of 'Customizing ReportNG reports' using optional system properties.

Build Tools: 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.