Execute TestNG Tests from ANT build.xml

We will create a sample project and execute TestNG tests using ANT build.xml. Before creating build.xml file, we will first create a project with some sample testng tests.

Before proceeding, please make sure you have installed ANT in your machine.

In Build.xml we will mainly see the below tags

1. project - Project is the staring point of the Ant configuration file and consists of the entire configuration with respect to building a project
2. target - A target is a set of tasks that that you want to execute during the build process.
3. task - Task is a piece of code that can be executed
4. property - Property tags are an important way to customize a build process or to provides way to define shortcuts to reuse repeatedly inside a build file

You can find more details of build file in Apache Ant manual

Step 1: Create a project

Step 2: Create sample packages as 'com.pack.one' and 'com.pack.two'

Step 3: Create multiple classes with in packages 'TestA' under 'com.pack.one' and 'TestB' under 'com.pack.two'

package com.pack.one;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestA {
	public void setUp() {
		System.out.println("*** In class - Test A ***");

	public void testOne() {
	public void testTwo() {
	public void tearDown() {
		System.out.println("*** End of class***");

Create class 'TestB' under 'com.pack.two'

package com.pack.two;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestB {
	public void setUp() {
		System.out.println("*** In class - Test B ***");

	public void testOne() {
	public void testTwo() {
	public void tearDown() {
		System.out.println("*** End of class***");

Step 4: Create testng.xml file and add the classes to test

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample Test Suite">
  <test name="Sample Test">
      <class name="com.pack.one.TestA"/>
       <class name="com.pack.two.TestB"/>

After following all the above steps, the project structure should look like below in the image. And after executing we will see the different project structure with reports folder getting created.

Ant Project Structure

To create build.xml file, Right click on the project -> New -> File -> Enter the file name and Click on Finish button.

And the below is the build.xml file.

<project name="Sample Ant build" basedir=".">
	<!-- ========== Initialize Properties =================================== -->
	<!-- set global properties for build -->
	<property name="basedir" value="." />
	<property name="lib" value="${basedir}/lib" />
	<property name="src" value="${basedir}/src" />
	<property name="bin" value="${basedir}/bin" />
	<property name="report-dir" value="${basedir}/Test-Report" />
	<property name="testng-report-dir" value="${report-dir}/TestNGreport" />
	<!-- ====== Set the classpath ====  -->
	<path id="classpath">
		<pathelement location="${bin}" />
		<fileset dir="${lib}">
			<include name="*.jar" />
	<!-- Delete directories  -->
	<target name="delete-dir">
		<delete dir="${bin}" />
		<delete dir="${report-dir}" />
	<!-- Creating directories -->
	<target name="create" depends="delete-dir">
		<mkdir dir="${bin}" />
		<mkdir dir="${report-dir}" />
	<!-- Compile the java code from ${src} into ${bin} -->
	<target name="compile" depends="create">
		<javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
		<echo> /* Compiled Directory Classes */ </echo>
	<!-- Runs the file and generates Reportng report for TestNG-->
	<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
	<target name="testng-execution" depends="compile">
		<mkdir dir="${testng-report-dir}" />
		<testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
			<xmlfileset dir="${basedir}" includes="testng.xml" />

You can also generate Select File -> click on Export -> General -> Ant Buildfiles and specify the file name, project, etc as per your needs.

generate build file

When executing, you may encounter an issue as 'tools.jar' does not exists. This is because, You have installed Java Runtime Environment (JRE) instead of the Java Development Kit (JDK). JRE doesn't have a tools.jar, We need to set JAVA_HOME and PATH variables and point to JDK, not to JRE.

When you run an Ant build from Eclipse by right clicking and selecting Ant Build, you should see a message as ''BUILD SUCCESSFUL" as below:

Buildfile: D:\Ant\SampleAntProject\build.xml
Total time: 903 milliseconds

How to run TestNG using ANT

First Open the command and navigate to the respective project path in the system. Under that folder, we need to enter the below command

ant testng-execution

Now it will show you the build.xml file that is getting executed and then it will compile and execute the TestNG tests. The below is the screen shot that you should observe.

Ant Build Success

Hope it helped you. Please let us know if you encounter any issues.

Build Tools: 



can you tell why i am getting the below mentioned error while following the steps mentioned above

[testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
[testng] at java.lang.Class.getDeclaredMethods0(Native Method)
[testng] at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
[testng] at java.lang.Class.getMethod0(Unknown Source)

go to /usr/java/jdk/lib
/usr/java/jdk/bin/unpack200 *.pack *.jar

Kindly let me know what and which step we need to do necessary changes in the above Build.xml file according to my project .
So that code will run without any error.
Can i change:-
1. Set Global property.
2.Set the classpath.

This post was really helpful for me. Thanks a lot.


Thank you so much for this! Very helpful :-)

I followed all of the above steps, the run in ant resulted in the following error: 'no suites, classes, methods or jar file was specified'. Any help would be greatly apreciated

Hi, how i can use this with webdriver ? How i can run selenium webserver / webdriver??


I am using Ant as a build tool for my selenium scripts .
I have multiple .properties file (eg. like qa.properties / prod.properties / perf.properties).I want to pass the parameters from each of the .properties file to ANT .how do i do it ?Please help

Thank you very much, this heped alot.

I'm facing below error.

Exception in thread "main" java.long.unsupportedClassVersionError: org/apache/tools/ant/launch/launcher : Unsupported major.minor version 52.0

I'm using Java (Jre7) for my selenium and Mozilla Firefox 33.0 for my testing. But my testA and testB doesn't invoke get URL to Mozilla firefox

I have run the testng.xml by testng suite and I could get the output. but while trying to run through the ANT I'm facing issues.

Add new comment