我們除了使用java來直接運行junit之外,我們還可以使用junit提供的junit task與ant結合來運行。
涉及的幾個主要的ant task如下:
<junit>,定義一個junit task |
運行Junit需要jakarta-ant-1.4-optional.jar和Junit.jar包,因為這兩個包用於支持ant task--<junit>的,所以不能在build.xml文件中加載,需要將他們放到ANT_HOME中的lib目錄中
junit.jar下載地址:http://pan.baidu.com/s/1hsbg464
jakarta-ant-1.4-optional.jar下載地址:http://pan.baidu.com/s/1hsjTXhM
完成上面的配置之后,開始編寫build.xml,並將其放置在項目的根目錄下。
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <project name="project" default="junit"> 4 <property name="run.classpath" value="bin"/> 5 <property name="run.srcpath" value="src"/> 6 <property name="test.xml" value="xml"/> 7 <property name="test.report" value="report"/> 8 <property name="lib.dir" value="lib"/> 9 10 <target name="init"> 11 <delete dir="${test.report}"/> 12 <mkdir dir="${test.report}"/> 13 <delete dir="${test.xml}"/> 14 <mkdir dir="${test.xml}"/> 15 </target> 16 17 <target name="compile" depends="init"> 18 <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/> 19 </target> 20 21 <!--Junit task--> 22 <target name="junit" depends="compile"> 23 <junit printsummary="false"> 24 <classpath path="${run.classpath}"/> 25 <formatter type="xml"/> 26 <batchtest todir="${test.xml}"> 27 <fileset dir="${run.classpath}"> 28 <!--運行${run.classpath}下所有和"**/*.class"匹配的用例--> 29 <include name="**/*.class"/> 30 </fileset> 31 </batchtest> 32 </junit> 33 <junitreport todir="${test.xml}"> 34 <fileset dir="${test.xml}"> 35 <include name="TEST-*.xml"/> 36 </fileset> 37 <report format="frames" todir="${test.report}"/> 38 </junitreport> 39 </target> 40 </project>
編寫junit case例子,注意需要繼承TestCase
1 package com.test.report; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Test; 6 7 import junit.framework.TestCase; 8 9 public class ANTTest extends TestCase { 10 11 int i = 0; 12 Boolean b = false; 13 14 @Test 15 public void test001() { 16 if (i == 0) { 17 b = true; 18 } else { 19 assertTrue("i is 0", b); 20 } 21 } 22 23 @Test 24 public void test002() { 25 if (i == 1) { 26 b = true; 27 } else { 28 assertTrue("i is 0", b); 29 } 30 } 31 32 }
進入項目根目錄,執行ant命令
進入根目錄下的report目錄,找到index.html文件
打開index.html查看測試結果
參考:
http://www.cnblogs.com/puresoul/p/4201565.html
http://blog.csdn.net/tochal/article/details/12560151
如果在運行testcase時需要依賴第三方包,那么build.xml需要被改成下面的內容
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <project name="project" default="junit"> 4 <property name="run.classpath" value="bin"/> 5 <property name="run.srcpath" value="src"/> 6 <property name="test.xml" value="xml"/> 7 <property name="test.report" value="report"/> 8 <property name="lib.dir" value="lib"/> 9 10 <path id="compile.path"> 11 <fileset dir="${lib.dir}"> 12 <include name="**/*.jar"/> 13 </fileset> 14 <pathelement path="${run.classpath}"/> 15 </path> 16 17 <target name="init"> 18 <delete dir="${test.report}"/> 19 <mkdir dir="${test.report}"/> 20 <delete dir="${test.xml}"/> 21 <mkdir dir="${test.xml}"/> 22 </target> 23 24 <target name="compile" depends="init"> 25 <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/> 26 </target> 27 28 <!--Junit task--> 29 <target name="junit" depends="compile"> 30 <junit printsummary="true"> 31 <classpath refid="compile.path"/> 32 <formatter type="xml"/> 33 <!--<test name="cn.com.vp4.hup.testcase.TestCase_AudioFocus"/> --> 34 <batchtest todir="${test.xml}"> 35 <fileset dir="${run.classpath}"> 36 <include name="**/*AudioFocus.class"/> 37 </fileset> 38 </batchtest> 39 </junit> 40 <junitreport todir="${test.xml}"> 41 <fileset dir="${test.xml}"> 42 <include name="TEST-*.xml"/> 43 </fileset> 44 <report format="frames" todir="${test.report}"/> 45 </junitreport> 46 </target> 47 </project>