English 中文(简体)
TestNG - Plug with ANT
  • 时间:2024-12-22

TestNG - Plug with ANT


Previous Page Next Page  

In this chapter, we will demonstrate how to run TestNG using ANT. Let s follow the steps given below −

Step 1: Download Apache Ant

Download the latest version of Apache Ant

OS Archive Name
Windows apache-ant-1.10.10-bin.zip
Linux apache-ant-1.10.10-bin.tar.gz
Mac apache-ant-1.10.10-bin.tar.gz

Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location, where ANT pbraries are stored on your machine. Let’s assume we ve stored the Ant pbraries in the folder apache-ant-1.8.4 folder.

OS Output
Windows Set the environment variable ANT_HOME to C:Program FilesApache Software Foundationapache-ant-1.10.10
Linux Export ANT_HOME=/usr/local/apache-ant-1.10.10
Mac Export ANT_HOME=/Library/apache-ant-1.10.10

Append Ant compiler location to System Path as follows −

OS Description
Windows Append the string %ANT_HOMEin at the end of the system variable, Path.
Linux export PATH=$PATH:$ANT_HOME/bin/
Mac Not required.

Step 3: Download TestNG Archive

Download the required jar files http://www.testng.org.

OS Archive name
Windows testng-7.4.jar
Linux testng-7.4.jar
Mac testng-7.4.jar

Step 4: Create Project Structure

    Create a folder TestNGWithAnt in /work/testng/src.

    Create a folder src in /work/testng/src/TestNGWithAnt.

    Create a folder test in /work/testng/src/TestNGWithAnt.

    Create a folder pb in /work/testng/src/TestNGWithAnt.

    Create MessageUtil class in /work/testng/src/TestNGWithAnt/src folder.

/*
* This class prints the given message on console.
*/

pubpc class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   pubpc MessageUtil(String message) {
      this.message = message;
   }

   // prints the message
   pubpc void printMessage() {
      System.out.println(message);
      return message;
   }

   // add "Hi!" to the message
   pubpc String salutationMessage() {
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }
}

    Create TestMessageUtil class in /work/testng/src/TestNGWithAnt/src folder.

import org.testng.Assert;
import org.testng.annotations.Test;


pubpc class TestMessageUtil {
   String message = "Manisha";
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   pubpc void testPrintMessage() {
      System.out.println("Inside testPrintMessage()");
      Assert.assertEquals(message,messageUtil.printMessage());
   }

   @Test
   pubpc void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
}

    Copy testng-7.4.jar in /work/testng/src/TestNGWithAnt/pb folder.

Create ANT build.xml

First, we need to define the TestNG Ant task as follows −

<taskdef name = "testng" classname = "org.testng.TestNGAntTask">
   <classpath>
      <pathelement location = "pb/testng-7.4.jar"/>
   </classpath>
</taskdef>

Then, we ll be using <testng> task in Ant to execute our TestNG test cases.

The build.xml file is as follows −

<project name = "TestNGTest" default = "test" basedir = ".">

   <!-- Define <testng> task -->

   <taskdef name = "testng" classname = "org.testng.TestNGAntTask">
      <classpath>
         <pathelement location = "pb/testng-7.4.jar"/>
      </classpath>
   </taskdef>

   <property name = "testdir" location = "test" />
   <property name = "srcdir" location = "src" />
   <property name = "pbdir" location = "pb" />
   <property name = "full-compile" value="true" />

   <path id = "classpath.base"/>
   <path id = "classpath.test">

   <fileset dir = "${pbdir}">
      <include name = "**/*.jar" />
   </fileset>

   <pathelement location = "${testdir}" />
   <pathelement location = "${srcdir}" />

   <path refid = "classpath.base" />
   </path>

   <target name = "clean" >
      <delete verbose="${full-compile}">
         <fileset dir = "${testdir}" includes="**/*.class" />
      </delete>
   </target>

   <target name = "compile" depends="clean">
      <javac srcdir = "${srcdir}" destdir = "${testdir}" verbose="${full-compile}">
         <classpath refid = "classpath.test"/>
      </javac>
   </target>

   <target name = "test" depends="compile">
      <testng outputdir = "${testdir}" classpathref="classpath.test">
         <xmlfileset dir = "${srcdir}" includes="testng.xml"/>
      </testng>
   </target>

</project>

Run the following Ant command.

/work/testng/src/TestNGWithAnt$ ant

Verify the output.

test:
   [testng] [TestNG] Running:
   [testng]   /work/testng/src/TestNGWithAnt/src/testng.xml
   [testng]
   [testng] Inside testPrintMessage()
   [testng] Manisha
   [testng] Inside testSalutationMessage()
   [testng] Hi!Manisha
   [testng]
   [testng] ===============================================
   [testng] Plug ANT test Suite
   [testng] Total tests run: 2, Failures: 0, Skips: 0
   [testng] ===============================================
   [testng]

BUILD SUCCESSFUL
Total time: 1 second
Advertisements