ant腳步執行JUnit測試用例

除了使用java來直接運行junit之外,我們還可以使用ant腳本,結合腳本執行junit用例,並生成測試報告,在進行每日構建等動作時非常有用。

一個完整的例子:

<?xml version="1.0"?>
<project name="project" default="test">
    <property name="build" value="bin"></property>
    <property name="src" value="src"></property>
    <property name="src-test" value="src-test"></property>
    <property name="lib" value="../lib"></property>
    <property name="testReport" value="testReport"></property>
    <path id="javac-lib">
	<fileset dir="${lib}">
		<include name="*.jar" />
	</fileset>
	
	<!--缺少以下的設置可能出現ClassNotFoundException錯誤-->
	<pathelement location="${build}"/>
    </path> 

    <target name="compile">
       <javac destdir="${build}" debug="true" encoding="UTF-8">
		<src path="src" />
		<classpath refid="javac-lib" />
	</javac>
    </target>    

    <target name="test" depends="compile"> 
	<delete dir="${testReport}" />
	<mkdir dir="${testReport}" />
 	
	<!--執行JUnit測試用例--> 
	<junit printsummary="yes"> 
		<classpath refid="javac-lib"/> 
		<formatter type="xml"/> 
		<batchtest todir="${testReport}"> 
			<fileset dir="${build}"> 
				<include name="**/*Test.class"/> 
			</fileset> 
		</batchtest> 
	</junit> 
	
	<!--生成html的測試報告--> 
	<junitreport todir="${testReport}"> 
		<fileset dir="${testReport}">
			<include name="TEST-*.xml"/> 
		</fileset> 
		<report format="frames" todir="${testReport}"/>
	</junitreport> 

	<!--刪除xml的測試結果-->
	<delete dir="${testReport}">
		<include name="*.xml" />
	</delete> 
    </target>
</project>


以上實例按幾個步驟執行:

1、設置了代碼路徑、編譯環境等
2、執行javac編譯target
3、執行junit測試target

其中junit target又按幾個步驟執行:
1、初始化報告輸出目錄
2、執行junit用例,'<junit printsummary="yes">'
3、<formatter type="xml"/>生成xml的測試結果
4、batchtest批量執行用例
5、junitreport根據每個xml用例執行結果生成html格式報告
6、刪除每個xml用例結果,如果想看生成xml格式結果,可以把這一步屏蔽

PS:
執行過程中發生ClassNotFoundException錯誤的解決辦法,注意在環境中是否加入了你的編譯路徑,<pathelement location="${build}"/>參考:http://yusudong.iteye.com/blog/1134915

JUnit Task詳細的參數以及各參數的描述可以參考官網的說明:http://ant.apache.org/manual/Tasks/junit.html


發佈了14 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章