To execute TestNG tests from ant task, following are the two steps.

  1. Define taskdef to add a TestNG task definition to the project, the implementation of the task available in testng.jar.
    <taskdef resource="testngtasks" classpath="testng.jar path"/>
    
  2. Define testng task with below attributes:
    1. workingDir: source code path to execute TestNG tests.
    2. outputDir : stores generated reports.
    3. classpathref : reference to the libraries and class files for the tests to be executed.
    4. haltOnfailure : stop the build process if a failure has occurred during the test run.
    5. xmlfileset : include the TestNG xml file for execution.
    <testng classpathref="classpath"  outputDir="${testng.report.dir}" workingDir="${src.dir}" haltOnfailure="true">
      <xmlfileset dir="${suites.dir}" includes="sample.xml"/>
    </testng>
    
Example to execute TestNG tests from Ant task

To execute tests from ant task, here are the prerequistes.

  • Create a sample TestNG class that has tests
  • Create a testng.xml that has classes needs to be executed
  • Build.xml file with testng ant task
  • Execute testng ant task
Create a sample TestNG class
    package com.exploreselenium;
    
    import org.apache.log4j.BasicConfigurator;
    import org.apache.log4j.Logger;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    public class SampleTests {
    
        static Logger logger=Logger.getLogger(TestBuild.class);
    
        @BeforeClass
        public void beforeClass(){
            BasicConfigurator.configure();
            logger.info("This is from Before Class method");
        }
    
        @Test
        public void firstTest(){
            logger.info("This is from firstTest()");
        }
    
    }
    
SAMPLE Testng.xml file

Created a testng.xml file named as sample.xml

  • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestNG Ant Task" verbose="1" >
        <test name="Sample" preserve-order="true">
            <classes>
                <class name="com.exploreselenium.SampleTests"/>
            </classes>
        </test>
    </suite>
    
Build.xml with ant task
  • Created a build.xml with testng ant task target named as ‘run-tests’.
  • Target ‘run-tests’ depends on ‘compile’ to compile the java code.
  • Target ‘compile’ depends on ‘init’ to delete and create bin directory.
  • Below is the complete build.xml file, you can directly use by just copy it.
  • <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    
    <project name="AntTest" default="run-tests" basedir=".">
        <property name="classes.dir" value="${basedir}/bin" />
        <property name="src.dir" value="${basedir}/src" />
        <property name="lib.dir" value="${basedir}/lib"/>
        <property name="testng.report.dir" value="${basedir}/test-output"/>
        <property name="suites.dir" value="${basedir}/config/Suites"/>
    
        <path id="classpath">
            <fileset dir="${lib.dir}">
                <include name="*.jar"/>
            </fileset>
            <pathelement path="${classes.dir}"/>
        </path>
    
        <target name="init">
            <delete dir="${classes.dir}" />
            <mkdir dir="${classes.dir}"/>
        </target>
    
        <target name="compile" depends="init">
            <javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true">
                <classpath refid="classpath"/>
            </javac>
        </target>
    
        <taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.4.jar"/>
    
        <target name="run-tests" depends="compile">
            <testng classpathref="classpath"  outputDir="${testng.report.dir}" workingDir="${src.dir}" haltOnfailure="true">
                <xmlfileset dir="${suites.dir}" includes="sample.xml"/>
            </testng>
        </target>
    
    </project>
    

It has a target ‘ant-tests‘ (<target name=”run-tests” depends=”compile”>) to run tests inside the classes that are given in testng.xml file.

In this target, mentioned ‘outputDir’ to store generated TestNG Reports.

Execute testng ant task

As discussed in previous Apache Ant Build Tool post, here is the command to execute ant targets,  ant <targetname>

  • ant run-test

When executed target, it compiles the java code and then executes the tests in SampleTests class.

Below is the output:

Testng ant task

Here is the project details for more details.

Project structure to run testng tests from ant task

 Challenges:

Some times while running TestNG tests from ant taks, might run into below issue.

  • [testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException

    [testng] Error: A JNI error has occurred, please check your installation and try again

 Problem:

  • The problem here is jcommander jar code was missed inside the TestNG jar. If you are using ant, JCommander should be inside testng.jar.
  • Here got the testng jar from Maven Central, but it didn’t contain all of TestNG’s dependencies.

Solution:

  • Download the jar directly from the TestNG site resolved the issue, as it contains the dependencies.

Finally, we learned on how to execute TestNG tests from ant task and its details so that we can execute tests from build xml file.

Run TestNG Tests
TestNG Parameters - Passing from XML
0