📜  TestNG-插入ANT

📅  最后修改于: 2020-11-10 05:02:50             🧑  作者: Mango


在本章中,我们将演示如何使用ANT运行TestNG。让我们按照下面给出的步骤-

步骤1:下载Apache Ant

下载最新版本的Apache Ant

OS Archive Name
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

步骤2:设定Ant环境

ANT_HOME环境变量设置为指向基本目录位置,该目录在您的计算机上存储ANT库。假设我们已经将Ant库存储在apache-ant-1.8.4文件夹中。

OS Output
Windows Set the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
Linux Export ANT_HOME=/usr/local/apache-ant-1.8.4
Mac Export ANT_HOME=/Library/apache-ant-1.8.4

将Ant编译器位置附加到系统路径,如下所示-

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

步骤3:下载TestNG存档

http://www.testng.org下载所需的jar文件

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

步骤4:创建项目结构

  • C语言创建一个文件夹TestNGWithAnt:\> TestNG_WORKSPACE。

  • C:\> TestNG_WORKSPACE> TestNGWithAnt中创建一个文件夹src

  • C:\> TestNG_WORKSPACE> TestNGWithAnt中创建一个文件夹测试

  • C:\> TestNG_WORKSPACE> TestNGWithAnt中创建一个文件夹

  • C:\> TestNG_WORKSPACE> TestNGWithAnt> src文件夹中创建MessageUtil类。

/*
* 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;
   }   
}      
  • C:\> TestNG_WORKSPACE> TestNGWithAnt> src文件夹中创建TestMessageUtil类。

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());
   }
}
  • 将testng-6.8.jar复制到C:\> TestNG_WORKSPACE> TestNGWithAnt> lib文件夹中。

创建ANT build.xml

首先,我们需要定义TestNG Ant任务,如下所示:


   
      
   

然后,我们将在Ant中使用任务来执行我们的TestNG测试用例。

build.xml文件如下-



   

   
      
         
      
   

   
   
   
   
   
   
   
   
   
      
   
   
   
   
   
   
   
   
   
      
         
      
   
   
   
      
         
      
   
   
   
       
          
      
   
   

运行以下Ant命令。

C:\TestNG_WORKSPACE\TestNGWithAnt>ant

验证输出。

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