最近因為工作關係,經常接觸 Java 專案。今天簡單紀錄一下使用 Ant 執行 JUnit 過程中,如何印出 JUnit 詳細執行結果,方便開發人員找出錯誤根本原因。
一般在 Target 內撰寫 Ant JUnit 會在 junit 標籤上 printsummary="yes" 與 showoutput="true" 屬性 ,輸出相關內容至銀幕上,如下列語法:
<junit printsummary="yes" showoutput="true" >
.....
</junit>
但在檢視結果時,只輸出了測試統計 (通過、失敗、錯誤數量),當錯誤發生時,沒有詳細列印出錯誤訊息,開發人員很難知道發生什麼錯誤。
unittest:
[junit] Running xxxx
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Test xxxxx FAILED
這個時候您可以使用 formatter 標籤輸出詳細資訊,其中:
- type : 可以為 plain (純文字) 或 xml 格式,後者可以用於 Azure DevOps 發佈測試結果
- usefile : 預設為 true (輸出至檔案)。設定為 false 則輸出至銀幕上
透過上列設定,發生錯誤時也會將 Exception 詳細印出,讓開發人員快速解決問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<target name="test" depends="build"> | |
<junit printsummary="yes" showoutput="true"> | |
<formatter type="plain" usefile="false" /><!-- 輸出至銀幕 --> | |
<formatter type="plain" /><!-- 輸出至檔案 --> | |
<formatter type="xml" /><!-- XML 格式輸出至檔案,適用於 Azure DevOps 發佈測試結果 --> | |
<batchtest> | |
<fileset dir="$./src" includes="**/*UnitTest.java"/> | |
</batchtest> | |
<classpath> | |
<pathelement location="./lib/junit-4.8.2.jar"/> | |
<pathelement location="./lib/ant-junit4.jar"/> | |
<pathelement location="./bin"/> | |
</classpath> | |
</junit> | |
</target> |
0 留言