Ant sample build file for TestNG

By xngo on February 28, 2019

Sample Ant build file to run TestNG

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Filename: build.xml
  Note: You have to change the followings according to your environment:
          -<pathelement location="lib/testng/testng-5.14.7.jar"/>
          -<pathelement location="bin"/>
-->
<project basedir="." default="runTestNG" name="Sample of Ant file for TestNG">
 
  <!-- Define <testng> task -->
  <taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
      <pathelement location="lib/testng/testng-5.14.7.jar"/>
    </classpath>
  </taskdef>
 
  <!-- Directory name where the TestNG report will be saved. -->
  <property name="testng.output.dir" value="testng_output"/>
 
  <!-- Directory path of compiled classes(i.e *.class) -->
  <path id="classes">
     <pathelement location="bin"/>
  </path>
 
  <!--
  Target to run TestNG. It will run according to what are defined in testng.xml.
  The report will be saved at .../testng_output/index.html.
  -->
  <target name="runTestNG">
 
    <mkdir dir="${testng.output.dir}"/><!-- Create the output directory. -->
 
    <testng outputdir="${testng.output.dir}" classpathref="classes" failureProperty="test.failure">
      <jvmarg value="-DMy.App.System.Property=123" />
      <xmlfileset dir="." includes="testng.xml"/> 
    </testng>
    <fail if="test.failure" message="Not all tests passed!" />
  </target>
 
</project>

Sample TestNG configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Filename: testng.xml
  Note: You have to change the following according to your environment:
          -<class name="com.packageName.MyTestClassName" />  
-->
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My Test Suite">
 
  <test name="My Test">
 
    <!--  Add all classes you would like TestNG to run. -->  
    <classes>
      <class name="com.packageName.MyTestClassName" />
    </classes>
 
  </test>
 
</suite>

Sample Structure of my files and directories created by Eclipse

C:.
¦   .classpath
¦   .project
¦   build.xml
¦   testng.xml
¦
+---bin
¦   +---com
¦       +---packageName
¦               MyTestClassName.class
¦
+---lib
¦   +---testng
¦           testng-5.14.7.jar
¦
+---src
¦   +---com
¦       +---packageName
¦               MyTestClassName.java
¦
+---testng_output
    ¦   emailable-report.html
    ¦   index.html
    ¦   testng-results.xml
    ¦   testng.css
    ¦
    +---junitreports
    ¦       TEST-com.packageName.MyTestClassName.xml
    ¦
    +---My Test Suite
            classes.html
            groups.html
            index.html
            main.html
            methods-alphabetical.html
            methods-not-run.html
            methods.html
            My Test.html
            My Test.properties
            My Test.xml
            reporter-output.html
            testng.xml.html
            toc.html

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.