Apache AntUnit
Apache AntUnit™
Apache AntUnit 是提供 Apache Ant 任務和類型測試架構的 Antlib。
Apache AntUnit 1.4.1
2021 年 7 月 7 日 - 發布 Apache AntUnit 1.4.1
Apache AntUnit 1.4.1 現已推出,可作為 二進位 或 原始碼 發行版下載。
此版本修正了 antlib.xml 描述符,讓 AntUnit 現在可以使用使用者定義的 URI,而不是硬編碼 AntUnit 的偏好 URI。
概念
最初,Apache Ant 任務的所有測試都寫成個別的 JUnit 測試案例。很快地,很明顯大多數測試都需要執行常見任務,例如讀取建置檔、使用建置檔初始化專案執行個體,以及執行目標。在這個時候,BuildFileTest 被發明出來,成為幾乎所有任務測試案例的基底類別。
BuildFileTest 運作良好,事實上也已被 Ant-Contrib 專案 和其他專案採用。
隨著時間推移,一種新的模式演變出來,越來越多的測試只執行目標,而不檢查任何效果。相反地,該目標包含了斷言,作為 <fail>
任務。以下是取自 ANTLR 任務建置檔的範例(使用 Ant 1.7 功能)
<target name="test3" depends="setup"> <antlr target="antlr.g" outputdirectory="${tmp.dir}"/> <fail> <condition> <!-- to prove each of these files exists; ANTLR >= 2.7.6 leaves behind new (.smap) files as well. --> <resourcecount when="ne" count="5"> <fileset dir="${tmp.dir}"> <include name="CalcParserTokenTypes.txt" /> <include name="CalcParserTokenTypes.java" /> <include name="CalcLexer.java" /> <include name="CalcParser.java" /> <include name="CalcTreeWalker.java" /> </fileset> </resourcecount> </condition> </fail> </target>
其中對應的 JUnit 測試案例已簡化為
... public class ANTLRTest extends BuildFileTest { private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/antlr/"; public ANTLRTest(String name) { super(name); } public void setUp() { configureProject(TASKDEFS_DIR + "antlr.xml"); } public void tearDown() { executeTarget("cleanup"); } public void test3() { executeTarget("test3"); } ... }
這種方法有幾個優點,其中之一是將錯誤報告中的範例建置檔轉換成測試案例非常容易。如果您要求使用者提供 Ant 中特定錯誤的測試案例,他們現在不再需要了解 JUnit 或如何將測試放入 Ant 的現有測試中。
AntUnit 採用這種方法進行更進一步的測試,它完全移除 JUnit,並附帶一組預先定義的 <assert>
任務,以便重複使用常見的檢查類型。
事實證明,AntUnit 也能作為其他問題的解決方案。例如,斷言是一種在開始建置程序之前驗證設定的簡便方法。AntUnit 也可以用於 Ant 任務範圍之外的功能性和整合測試(在執行應用程式後斷言資料庫的內容、斷言 HTTP 回應的內容……)。這是一個需要更多研究的領域。
概念
antunit 任務
就像 <junit> 對 JUnit 測試一樣,<antunit> 任務會驅動測試。
當在建置檔案上呼叫時,任務會為該建置檔案啟動新的 Ant 專案,並掃描名稱以「test」開頭的目標。對於每個此類目標,它會
- 如果存在,執行名為 setUp 的目標。
- 執行目標本身 - 如果此目標依賴於其他目標,則套用正常的 Ant 規則,並先執行依賴目標。
- 如果存在,執行名為 tearDown 的目標。
斷言
基本任務是 <assertTrue>
。它接受一個巢狀條件,如果該條件評估為 false,則會擲出名為 AssertionFailedException 的 BuildException 子類別。
這個任務可以使用 <macrodef>
和 <fail>
來實作,但事實上它是一個「真實的」任務,因此可以擲出 BuildException 子類別。<antunit>
任務會捕捉此例外狀況,並將目標標記為失敗,任何其他類型的例外狀況(包括其他 BuildException)都是測試錯誤。
除了 <assertTrue>
之外,還有許多針對一般條件的預定義斷言,其中大多數只是巨集。
其他任務
<logcapturer>
會擷取通過 Ant 記錄系統的所有訊息,並透過專案內的參考提供這些訊息。如果您想斷言某些記錄訊息,則需要啟動此任務(在測試目標之前),並使用 <assertLogContains>
斷言。
<expectFailure>
是任務容器,它會捕捉巢狀任務擲出的任何 BuildException。如果沒有擲出例外狀況,它會導致測試失敗(透過擲出 AssertionFailedException)。
AntUnitListener
程式庫的一部分是 AntUnitListener
介面,可用於記錄測試結果。<antunit> 任務接受任意多個偵聽器,並將測試結果傳遞給它們。
目前有兩個實作 - <plainlistener>
和 xmllistener
,它們是根據「plain」和「xml」JUnit 偵聽器建模的 - 與程式庫綑綁在一起。
範例
這是測試 <touch>
是否會在檔案不存在時建立檔案的方法
<project xmlns:au="antlib:org.apache.ant.antunit"> <!-- is called prior to the test --> <target name="setUp"> <property name="foo" value="foo"/> </target> <!-- is called after the test, even if that caused an error --> <target name="tearDown"> <delete file="${foo}" quiet="true"/> </target> <!-- the actual test case --> <target name="testTouchCreatesFile"> <au:assertFileDoesntExist file="${foo}"/> <touch file="${foo}"/> <au:assertFileExists file="${foo}"/> </target> </project>
當執行類似下列任務時
<au:antunit> <fileset dir="." includes="touch.xml"/> <au:plainlistener/> </au:antunit>
從自己的建置檔中,您會取得類似下列的結果
[au:antunit] Build File: /tmp/touch.xml [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec [au:antunit] Target: testTouchCreatesFile took 0.183 sec BUILD SUCCESSFUL Total time: 1 second