- 测试NG教程
 - 测试NG - 主页
 - TestNG - 概述
 - TestNG - 环境
 - TestNG - 编写测试
 - TestNG - 基本注释
 - TestNG - 执行过程
 - TestNG - 执行测试
 - TestNG - 套件测试
 - TestNG - 忽略测试
 - TestNG - 小组测试
 - TestNG - 异常测试
 - TestNG - 依赖性测试
 - TestNG - 参数化测试
 - TestNG - 运行 JUnit 测试
 - TestNG - 测试结果
 - TestNG - 注释变压器
 - TestNG - 断言
 - TestNG - 并行执行
 - TestNG - 带 ANT 的插头
 - TestNG - 使用 Eclipse 进行插件
 - TestNG - TestNG - 与 JUnit
 
- TestNG 有用资源
 - TestNG - 快速指南
 - TestNG - 有用的资源
 - TestNG - 讨论
 
TestNG - 带 ANT 的插头
在本章中,我们将演示如何使用 ANT 运行 TestNG。让我们按照下面给出的步骤操作 -
第 1 步:下载 Apache Ant
下载最新版本的Apache Ant
| 操作系统 | 档案名称 | 
|---|---|
| Windows | apache-ant-1.10.10-bin.zip | 
| Linux | apache-ant-1.10.10-bin.tar.gz | 
| 苹果 | apache-ant-1.10.10-bin.tar.gz | 
第二步:设置Ant环境
将ANT_HOME环境变量设置为指向计算机上存储 ANT 库的基本目录位置。假设我们已将 Ant 库存储在 apache-ant-1.8.4 文件夹中。
| 操作系统 | 输出 | 
|---|---|
| Windows | 将环境变量 ANT_HOME 设置为 C:\Program Files\Apache Software Foundation\apache-ant-1.10.10 | 
| Linux | 导出 ANT_HOME=/usr/local/apache-ant-1.10.10 | 
| 苹果 | 导出 ANT_HOME=/Library/apache-ant-1.10.10 | 
将 Ant 编译器位置附加到系统路径,如下所示 -
| 操作系统 | 描述 | 
|---|---|
| Windows | 将字符串 %ANT_HOME\bin 附加到系统变量 Path 的末尾。 | 
| Linux | 导出 PATH=$PATH:$ANT_HOME/bin/ | 
| 苹果 | 不需要。 | 
第 3 步:下载 TestNG 存档
下载所需的 jar 文件http://www.testng.org。
| 操作系统 | 档案名称 | 
|---|---|
| Windows | testng-7.4.jar | 
| Linux | testng-7.4.jar | 
| 苹果 | testng-7.4.jar | 
第 4 步:创建项目结构
在/work/testng/src中创建文件夹TestNGWithAnt。
在/work/testng/src/TestNGWithAnt中创建文件夹src。
在/work/testng/src/TestNGWithAnt中创建文件夹test。
在/work/testng/src/TestNGWithAnt中创建文件夹lib。
在/work/testng/src/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;
   }
}
在/work/testng/src/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-7.4.jar 复制到/work/testng/src/TestNGWithAnt/lib文件夹中。
创建 ANT build.xml
首先,我们需要定义 TestNG Ant 任务,如下所示 -
<taskdef name = "testng" classname = "org.testng.TestNGAntTask">
   <classpath>
      <pathelement location = "lib/testng-7.4.jar"/>
   </classpath>
</taskdef>
然后,我们将使用Ant 中的<testng>任务来执行 TestNG 测试用例。
build.xml文件如下 -
<project name = "TestNGTest" default = "test" basedir = ".">
   <!-- Define <testng> task -->
   <taskdef name = "testng" classname = "org.testng.TestNGAntTask">
      <classpath>
         <pathelement location = "lib/testng-7.4.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>
运行以下 Ant 命令。
/work/testng/src/TestNGWithAnt$ ant
验证输出。
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