TestNG - Pasang dengan ANT

Dalam bab ini, kami akan mendemonstrasikan cara menjalankan TestNG menggunakan ANT. Mari ikuti langkah-langkah yang diberikan di bawah ini -

Langkah 1: Unduh Apache Ant

Unduh Apache Ant versi terbaru

OS Nama Arsip
Windows apache-ant-1.8.4-bin.zip
Linux apache-ant-1.8.4-bin.tar.gz
Mac apache-ant-1.8.4-bin.tar.gz

Langkah 2: Atur Lingkungan Semut

Mengatur ANT_HOMEvariabel lingkungan untuk menunjuk ke lokasi direktori dasar, tempat pustaka ANT disimpan di komputer Anda. Mari kita asumsikan kita telah menyimpan perpustakaan Ant di folder apache-ant-1.8.4.

OS Keluaran
Windows Setel variabel lingkungan ANT_HOME ke C: \ Program Files \ Apache Software Foundation \ apache-ant-1.8.4
Linux Ekspor ANT_HOME = / usr / local / apache-ant-1.8.4
Mac Ekspor ANT_HOME = / Library / apache-ant-1.8.4

Tambahkan lokasi kompilator Ant ke Jalur Sistem sebagai berikut -

OS Deskripsi
Windows Tambahkan string% ANT_HOME \ bin di akhir variabel sistem, Path.
Linux Ekspor PATH = $ PATH: $ ANT_HOME / bin /
Mac Tidak dibutuhkan.

Langkah 3: Unduh Arsip TestNG

Unduh file jar yang diperlukan http://www.testng.org.

OS Nama arsip
Windows testng-6.8.jar
Linux testng-6.8.jar
Mac testng-6.8.jar

Langkah 4: Buat Struktur Proyek

  • Buat folder TestNGWithAnt di C:\>TestNG_WORKSPACE.

  • Buat folder src di C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Buat folder test di C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Buat folder lib di C:\>TestNG_WORKSPACE>TestNGWithAnt.

  • Membuat MessageUtil kelas di C:\>TestNG_WORKSPACE>TestNGWithAnt>src map.

/*
* 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;
   }   
}
  • Buat kelas TestMessageUtil di C:\>TestNG_WORKSPACE>TestNGWithAnt>src map.

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());
   }
}
  • Salin testng-6.8.jar di C:\>TestNG_WORKSPACE>TestNGWithAnt>lib map.

Buat ANT build.xml

Pertama, kita perlu mendefinisikan tugas Ant TestNG sebagai berikut -

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

Kemudian, kami akan menggunakan <testng> tugas di Ant untuk menjalankan kasus uji TestNG kami.

Itu build.xml file adalah sebagai berikut -

<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>

Jalankan perintah Ant berikut.

C:\TestNG_WORKSPACE\TestNGWithAnt>ant

Verifikasi hasilnya.

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