- TestNG 教程
- TestNG - 首頁
- 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 |
| Mac | apache-ant-1.10.10-bin.tar.gz |
步驟 2:設定 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 | Export ANT_HOME=/usr/local/apache-ant-1.10.10 |
| Mac | Export ANT_HOME=/Library/apache-ant-1.10.10 |
將 Ant 編譯器位置追加到系統路徑,如下所示:
| 作業系統 | 描述 |
|---|---|
| Windows | 在系統變數 Path 的末尾追加字串 %ANT_HOME\bin。 |
| Linux | export PATH=$PATH:$ANT_HOME/bin/ |
| Mac | 無需操作。 |
步驟 3:下載 TestNG 歸檔檔案
下載所需的 jar 檔案 http://www.testng.org.
| 作業系統 | 歸檔名稱 |
|---|---|
| Windows | testng-7.4.jar |
| Linux | testng-7.4.jar |
| Mac | 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
廣告