পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 24, 2017

TestNG - Plug with ANT

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

OSArchive Name
Windowsapache-ant-1.8.4-bin.zip
Linuxapache-ant-1.8.4-bin.tar.gz
Macapache-ant-1.8.4-bin.tar.gz

Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location, where ANT libraries are stored on your machine. Let’s assume we've stored the Ant libraries in the folder apache-ant-1.8.4 folder.
OSOutput
WindowsSet the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
LinuxExport ANT_HOME=/usr/local/apache-ant-1.8.4
MacExport ANT_HOME=/Library/apache-ant-1.8.4
Append Ant compiler location to System Path as follows:
OSDescription
WindowsAppend the string %ANT_HOME\bin at the end of the system variable, Path.
LinuxExport PATH=$PATH:$ANT_HOME/bin/
MacNot required.

Step 3: Download TestNG Archive

Download the required jar files from://www.testng.org.
OSArchive name
Windowstestng-6.8.jar
Linuxtestng-6.8.jar
Mactestng-6.8.jar

Step 4: Create Project Structure

  • Create a folder TestNGWithAnt in C:\>TestNG_WORKSPACE.
  • Create a folder src in C:\>TestNG_WORKSPACE>TestNGWithAnt.
  • Create a folder test in C:\>TestNG_WORKSPACE>TestNGWithAnt.
  • Create a folder lib in C:\>TestNG_WORKSPACE>TestNGWithAnt.
  • Create MessageUtil class in C:\>TestNG_WORKSPACE>TestNGWithAnt>src folder.
/*
* This class prints the given message on console.
*/

public class MessageUtil {

   private String message;

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

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

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}   
  • Create TestMessageUtil class in C:\>TestNG_WORKSPACE>TestNGWithAnt>src folder.
import org.testng.Assert;
import org.testng.annotations.Test;


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

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

   @Test
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
}
  • Copy testng-6.8.jar in C:\>TestNG_WORKSPACE>TestNGWithAnt>lib 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="lib/testng-6.8.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="lib/testng-6.8.jar"/>
      </classpath>
   </taskdef>

   <property name="testdir" location="test" />
   <property name="srcdir" location="src" />
   <property name="libdir" location="lib" />
   <property name="full-compile" value="true" />
   
   <path id="classpath.base"/>
   <path id="classpath.test">
   
   <fileset dir="${libdir}">
      <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.
C:\TestNG_WORKSPACE\TestNGWithAnt>ant
Verify the output.
test:
   [testng] [TestNG] Running:
   [testng]   C:\TestNG_WORKSPACE\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

No comments:

Post a Comment