一、ANT任務之Junit:
學習ANT其實主要是學習ANT的task,ANT眾多task中有一個Testing Tasks,它下面有兩個任務:Junit和JunitReport,主要用來進行單元測試及生成單元測試報告。
Testing Tasks |
---|
Task Name | Description |
---|---|
Junit | Runs tests from the Junit testing framework. This task has been tested with JUnit 3.0 up to JUnit 3.7; it won't work with versions prior to JUnit 3.0. |
JunitReport | Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to provide a browsable report of the testcases results. |
官方網址:http://ant.apache.org/manual/index.html
<junit>下面可以包含其它元素,例如:
1、<test>:運行單個TestCase
2、<batchtest>:運行多個TestCase
3、<formatter>:定義測試結果輸出格式
還有很多,詳細可以參考官方文檔。
二、項目實例:
由於ant安裝比較得簡單,網上一搜一大把且現在ecplise基本都帶ant,所以本文並未說明如何搭建ant環境。
另外,在eclipse中可以通過:window->show view 來調出Ant視圖
1、目錄結構如下:
2、SimpleCalculation類代碼如下:

1 package com.glen.he; 2 3 public class SimpleCalculation { 4 public int Add(int a,int b){ 5 return (a+b); 6 } 7 8 }
3、測試類SimpleCalculationTest代碼如下:

1 package com.glen.he; 2 3 import com.glen.he.SimpleCalculation; 4 5 import static org.junit.Assert.*; 6 import org.junit.Test; 7 8 public class SimpleCalculationTest { 9 10 SimpleCalculation sc = new SimpleCalculation(); 11 12 @Test 13 public void AddTest() { 14 15 int c = sc.Add(3, 5); 16 17 assertEquals(8, c); 18 } 19 }
4、在項目要目錄下添加build.xml(執行一個測試)文件,內容如下:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="AntDemo" default="junit" basedir="."> 3 <!-- =================================================================== --> 4 <!-- 變量設置 --> 5 <!-- =================================================================== --> 6 7 <!-- 源代碼src路徑 --> 8 <property name="src.path" value="src/java"/> 9 <!-- 編譯文件class路徑 --> 10 <property name="build.path" value="build"/> 11 <!-- 單元測試代碼路徑 --> 12 <property name="test.path" value="src/test"/> 13 <!-- lib包路徑 --> 14 <property name="lib.path" value="lib"/> 15 16 <!-- =================================================================== --> 17 <!-- 設置classpath --> 18 <!-- =================================================================== --> 19 <path id="compile.path"> 20 <fileset dir="${lib.path}"> 21 <include name="**/*.jar"/> 22 </fileset> 23 24 <pathelement path="${build.path}"/> 25 </path> 26 27 <!-- =================================================================== --> 28 <!-- 清除歷史編譯class --> 29 <!-- =================================================================== --> 30 <target name="clean" description="clean"> 31 <delete dir="${build.path}"/> 32 </target> 33 34 <!-- =================================================================== --> 35 <!-- 編譯測試文件,初始化目錄 --> 36 <!-- =================================================================== --> 37 <target name="compile" description="compile"> 38 <mkdir dir="${build.path}"/> 39 <javac srcdir="${src.path}" destdir="${build.path}" classpathref="compile.path"/> 40 <javac srcdir="${test.path}" destdir="${build.path}" classpathref="compile.path"/> 41 </target> 42 43 <!-- =================================================================== --> 44 <!-- 執行測試案例 --> 45 <!-- =================================================================== --> 46 <target name="junit" depends="clean,compile"> 47 <junit printsummary="true"> 48 <classpath refid="compile.path"/> 49 50 <test name="com.glen.he.SimpleCalculationTest"/> 51 </junit> 52 </target> 53 54 </project>
說明:
<junit printsummary="true">
<classpath refid="compile.path"/>
<test name="com.glen.he.SimpleCalculationTest"/>
</junit>
<path id="compile.path">
<fileset dir="${lib.path}">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${build.path}"/>
</path
我們在<junit〉任務下,使用了編譯后的.class文件的目錄,還有編譯所需的jar包所在的目錄。 因為,JUnit任務實際就是為我們運行Test類,而不僅僅是像我們發布Ant文件那樣只是javac編譯,只需要編譯所需的Jar包。我們還需要像java任務那樣運.class文件,所以必須包括編譯后的.class文件。
5、然后把build.xml文件拖到Ant視圖中,如下圖,雙擊junit執行即可。
6、執行結果:
1 Buildfile: D:\AntTest\build.xml
2 clean:
3 [delete] Deleting directory D:\AntTest\build
4 compile:
5 [mkdir] Created dir: D:\AntTest\build
6 [javac] Compiling 1 source file to D:\AntTest\build
7
8 [javac] Compiling 1 source file to D:\AntTest\build
9 junit:
10 [junit] Running com.glen.he.SimpleCalculationTest
11 [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.016 sec
12 BUILD SUCCESSFUL
13 Total time: 1 second
三、增強版build.xml
通過上面第二步,基本可以達到使用ant和junit來進行單元測試,但還遠遠不夠,比如需要批量運行案例,生成報告等,下面會介紹這些內容
1、使用formatter屬性輸出junit信息:
- 修改build.xml文件,增加第16,49,51,57,58,59行代碼
- 修改build.xml文件,修改53行代碼,增加了todir屬性,指定xml的輸出路徑。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="AntDemo" default="junit" basedir="."> 3 <!-- =================================================================== --> 4 <!-- 變量設置 --> 5 <!-- =================================================================== --> 6 7 <!-- 源代碼src路徑 --> 8 <property name="src.path" value="src/java"/> 9 <!-- 編譯文件class路徑 --> 10 <property name="build.path" value="build"/> 11 <!-- 單元測試代碼路徑 --> 12 <property name="test.path" value="src/test"/> 13 <!-- lib包路徑 --> 14 <property name="lib.path" value="lib"/> 15 <!-- 生成報告junit4.xml路徑 --> 16 <property name="report.path" value="report"/> 17 18 <!-- =================================================================== --> 19 <!-- 設置classpath --> 20 <!-- =================================================================== --> 21 <path id="compile.path"> 22 <fileset dir="${lib.path}"> 23 <include name="**/*.jar"/> 24 </fileset> 25 26 <pathelement path="${build.path}"/> 27 </path> 28 29 <!-- =================================================================== --> 30 <!-- 清除歷史編譯class --> 31 <!-- =================================================================== --> 32 <target name="clean" description="clean"> 33 <delete dir="${build.path}"/> 34 </target> 35 36 <!-- =================================================================== --> 37 <!-- 編譯測試文件,初始化目錄 --> 38 <!-- =================================================================== --> 39 <target name="compile" description="compile"> 40 <mkdir dir="${build.path}"/> 41 <javac srcdir="${src.path}" destdir="${build.path}" classpathref="compile.path"/> 42 <javac srcdir="${test.path}" destdir="${build.path}" classpathref="compile.path"/> 43 </target> 44 45 <!-- =================================================================== --> 46 <!-- 執行測試案例 --> 47 <!-- =================================================================== --> 48 <target name="junit" depends="clean,compile"> 49 <mkdir dir="${report.path}"/> 50 <junit printsummary="true" fork="true"> 51 <formatter type="xml" usefile="true"/> 52 <classpath refid="compile.path"/> 53 <test name="com.glen.he.SimpleCalculationTest" todir="${report.path}" fork="true"/> 54 </junit> 55 </target> 56 57 <target name="delete"> 58 <delete dir="${report.path}"/> 59 </target> 60 61 </project>
執行junit的task后,在項目report目錄下生成了一個名為TEST-com.glen.he.SimpleCalculationTest.xml的xml文件。
另外:
<formatter type="xml" usefile="true"/>中type屬性值還有plain、brief
這時會輸出一個文本文件,提供測試失敗時的詳細內容以及每個測試的運行統計。
2、批量運行單元測試案例:
- 修改build.xml文件,把步驟7中的第53行代碼替換成下面的58~62行代碼;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="AntDemo" default="junit" basedir="."> 3 <!-- =================================================================== --> 4 <!-- 變量設置 --> 5 <!-- =================================================================== --> 6 7 <!-- 源代碼src路徑 --> 8 <property name="src.path" value="src/java"/> 9 <!-- 編譯文件class路徑 --> 10 <property name="build.path" value="build"/> 11 <!-- 單元測試代碼路徑 --> 12 <property name="test.path" value="src/test"/> 13 <!-- lib包路徑 --> 14 <property name="lib.path" value="lib"/> 15 <!-- 生成報告junit4.xml路徑 --> 16 <property name="report.path" value="report"/> 17 18 <!-- =================================================================== --> 19 <!-- 設置classpath --> 20 <!-- =================================================================== --> 21 <path id="compile.path"> 22 <fileset dir="${lib.path}"> 23 <include name="**/*.jar"/> 24 </fileset> 25 26 <pathelement path="${build.path}"/> 27 </path> 28 29 <target name="init"> 30 <mkdir dir="${build.path}"/> 31 <mkdir dir="${report.path}"/> 32 </target> 33 34 <!-- =================================================================== --> 35 <!-- 清除歷史編譯class --> 36 <!-- =================================================================== --> 37 <target name="clean" description="clean"> 38 <delete dir="${build.path}"/> 39 </target> 40 41 <!-- =================================================================== --> 42 <!-- 編譯測試文件,初始化目錄 --> 43 <!-- =================================================================== --> 44 <target name="compile" depends="init" description="compile"> 45 <javac srcdir="${src.path}" destdir="${build.path}" classpathref="compile.path"/> 46 <javac srcdir="${test.path}" destdir="${build.path}" classpathref="compile.path"/> 47 </target> 48 49 <!-- =================================================================== --> 50 <!-- 執行測試案例 --> 51 <!-- =================================================================== --> 52 <target name="junit" depends="compile"> 53 <junit printsummary="true" fork="true"> 54 <formatter type="xml" usefile="true"/> 55 56 <classpath refid="compile.path"/> 57 58 <batchtest fork="on" todir="${report.path}" haltonfailure="no"> 59 <fileset dir="${build.path}"> 60 <include name="**/*Test.class"/> 61 </fileset> 62 </batchtest> 63 64 </junit> 65 66 </target> 67 68 <!-- 清除Junit生成的報表文檔 --> 69 <target name="delete"> 70 <delete dir="${report.path}"/> 71 </target> 72 73 </project>
3、使用<JunitReport>生成測試報告:
在上面我們已經知道,通過formatter(type=“xml”)輸出junit信息時會在指定目錄下生成一個Test-類路徑名.xml的xml文件,但是這個xml文件看起來很不方便。Ant提供了<junitreport>任務使用XSLT將xml文件轉換為HTML報告,該任務首先將生成的XML文件整合成單一的XML文件,然后再對他進行轉換,這個整合的文件默認情況下被命名為:TESTS-TestSuites.xml.
- 修改上面的build.xml文件,增加65~71行,如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="AntDemo" default="junit" basedir="."> 3 <!-- =================================================================== --> 4 <!-- 變量設置 --> 5 <!-- =================================================================== --> 6 7 <!-- 源代碼src路徑 --> 8 <property name="src.path" value="src/java"/> 9 <!-- 編譯文件class路徑 --> 10 <property name="build.path" value="build"/> 11 <!-- 單元測試代碼路徑 --> 12 <property name="test.path" value="src/test"/> 13 <!-- lib包路徑 --> 14 <property name="lib.path" value="lib"/> 15 <!-- 生成報告junit4.xml路徑 --> 16 <property name="report.path" value="report"/> 17 18 <!-- =================================================================== --> 19 <!-- 設置classpath --> 20 <!-- =================================================================== --> 21 <path id="compile.path"> 22 <fileset dir="${lib.path}"> 23 <include name="**/*.jar"/> 24 </fileset> 25 26 <pathelement path="${build.path}"/> 27 </path> 28 29 <target name="init"> 30 <mkdir dir="${build.path}"/> 31 <mkdir dir="${report.path}"/> 32 </target> 33 34 <!-- =================================================================== --> 35 <!-- 清除歷史編譯class --> 36 <!-- =================================================================== --> 37 <target name="clean" description="clean"> 38 <delete dir="${build.path}"/> 39 </target> 40 41 <!-- =================================================================== --> 42 <!-- 編譯測試文件,初始化目錄 --> 43 <!-- =================================================================== --> 44 <target name="compile" depends="init" description="compile"> 45 <javac srcdir="${src.path}" destdir="${build.path}" classpathref="compile.path"/> 46 <javac srcdir="${test.path}" destdir="${build.path}" classpathref="compile.path"/> 47 </target> 48 49 <!-- =================================================================== --> 50 <!-- 執行測試案例 --> 51 <!-- =================================================================== --> 52 <target name="junit" depends="compile"> 53 <junit printsummary="true" fork="true"> 54 <formatter type="xml" usefile="true"/> 55 56 <classpath refid="compile.path"/> 57 58 <batchtest fork="on" todir="${report.path}" haltonfailure="no"> 59 <fileset dir="${build.path}"> 60 <include name="**/*Test.class"/> 61 </fileset> 62 </batchtest> 63 </junit> 64 65 <!-- 產生單元測試報表文檔 --> 66 <junitreport todir="${report.path}"> 67 <fileset dir="${report.path}"> 68 <include name="TEST-*.xml" /> 69 </fileset> 70 <report format="frames" todir="${report.path}" /> 71 </junitreport> 72 73 </target> 74 75 <!-- 清除Junit生成的報表文檔 --> 76 <target name="delete"> 77 <delete dir="${report.path}"/> 78 </target> 79 80 </project>
執行后會在指定目錄下生成報告文檔,打開index.html可以很方便的看到執行的結果。
1、如下圖所示(我又補充了3個案例,並且故意讓兩個案例失敗),顯示執行的統計結果:
2、點擊classes下面的ComplexCalculationTest,可以看到具體某個類里面的單元測試案例執行統計情況以及失敗案例的錯誤信息提示。
OVER!
參考:
http://blog.csdn.net/shendl/article/details/532587
http://blog.csdn.net/tochal/article/details/12560151