在本章中,我们将演示如何使用ANT运行TestNG.让我们按照下面给出的步骤 : 去;
下载最新版本的 Apache Ant
OS | 存档名称 |
---|---|
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 |
设置 ANT_HOME 环境变量指向基目录位置,其中ANT库存储在您的计算机上.假设我们已将Ant库存储在文件夹apache-ant-1.8.4文件夹中.
OS | 输出 |
---|---|
Windows | 将环境变量ANT_HOME设置为C:\Program Files \ ApachePache Foundation \apache -ant-1.8.4 |
Linux | 导出ANT_HOME =/usr/local/apache-ant-1.8.4 |
Mac | 导出ANT_HOME =/Library/apache-ant-1.8.4 |
追加Ant编译器系统路径的位置如下 :
OS | 描述 |
---|---|
Windows | 追加字符串%ANT_HO ME \ bin在系统变量的末尾,路径. |
Linux | 导出PATH = $ PATH:$ ANT_HOME/bin/ |
Mac | 不需要. |
下载所需的jar文件 http://www.testng.org.
OS | 存档名称 |
---|---|
Windows | testng-6.8.jar |
Linux | testng-6.8.jar |
Mac | testng-6.8.jar |
在 C:\> Tes中创建文件夹 TestNGWithAnt tNG_WORKSPACE .
在 C:\> TestNG_WORKSPACE> TestNGWithAnt中创建文件夹 src .
在 C:\> TestNG_WORKSPACE> TestNGWithAnt 中创建文件夹 test .
在 C:\> TestNG_WORKSPACE> TestNGWithAnt 中创建 lib 文件夹.
在 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; } }
在中创建TestMessageUtil类C:\> TestNG_WORKSPACE> TestNGWithAnt> src 文件夹.
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 in C:\> TestNG_WORKSPACE> TestNGWithAnt> lib 文件夹.
首先,我们需要将TestNG Ant任务定义如下 :
<taskdef name = "testng" classname = "org.testng.TestNGAntTask"> <classpath> <pathelement location = "lib/testng-6.8.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-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>
运行以下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