結 合Ant與JUnit,可以讓你進行自動測試,最基本的方式可以執行org.junit.runner.JUnitCore來 進行測試。例如:
<project name="example" default="run">
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="junit.dir" value="lib/junit-4.8.2.jar"/>
<property name="test.class" value=""/>
<target name="clean">
<delete dir="${bin.dir}"/>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${bin.dir}"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${junit.dir}"/>
</classpath>
</javac>
</target>
<target name="run" depends="compile">
<java classname="org.junit.runner.JUnitCore">
<classpath path="${bin.dir};${junit.dir}"/>
<arg value="${test.class}"/>
</java>
</target>
</project>
在執行時,可以指定測試類別。例如:
>ant
-Dtest.class=cc.openhome.CalculatorTest
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:16: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to C:\workspace\example\bin
run:
[java] JUnit version 4.8.2
[java] ..
[java] Time: 0.015
[java]
[java] OK (2 tests)
[java]
BUILD SUCCESSFUL
Total time: 1 second
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:16: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to C:\workspace\example\bin
run:
[java] JUnit version 4.8.2
[java] ..
[java] Time: 0.015
[java]
[java] OK (2 tests)
[java]
BUILD SUCCESSFUL
Total time: 1 second
你也可以使用<junit>標 籤,要使用這個標籤,Ant必須知道JUnit的JAR檔位置,你可以將JUnit的JAR檔放在Ant的lib目錄中,或者是於<junit>中使用 <classpath>指定。例如:
<project name="example" default="test">
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="junit.dir" value="lib/junit-4.8.2.jar"/>
<target name="clean">
<delete dir="${bin.dir}"/>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${bin.dir}"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${junit.dir}"/>
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes">
<test name="cc.openhome.CalculatorTest"/>
<classpath>
<pathelement location="${bin.dir}"/>
<pathelement location="${junit.dir}"/>
</classpath>
</junit>
</target>
</project>
執行時可以如下:
>ant
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:16: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to C:\workspace\example\bin
test:
[junit] Running cc.openhome.CalculatorTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.028 sec
BUILD SUCCESSFUL
Total time: 1 second
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:16: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to C:\workspace\example\bin
test:
[junit] Running cc.openhome.CalculatorTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.028 sec
BUILD SUCCESSFUL
Total time: 1 second
你可以在<junit>標 籤上指定其它屬性:
<junit printsummary="yes"
haltonerror="yes" haltonfailure="yes" fork="yes">
haltonerror與haltonfailure如果設為yes,則在Error或Failure時,會中斷測試,fork如果為yes,則會為每個測試啟動一個新的JVM。
如果一次執行的測試類別不只一個,使用<test>指 定並不方便,則可以使用<batchtest>。 例如指定所有Test結尾的測試類別:
<project name="example" default="test">
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="junit.dir" value="lib/junit-4.8.2.jar"/>
<target name="clean">
<delete dir="${bin.dir}"/>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${bin.dir}"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${junit.dir}"/>
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes">
<batchtest>
<fileset dir="${src.dir}" includes="**/*Test.java"/>
</batchtest>
<classpath>
<pathelement location="${bin.dir}"/>
<pathelement location="${junit.dir}"/>
</classpath>
</junit>
</target>
</project>
執行時可以如下:
>ant
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:15: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 3 source files to C:\workspace\example\bin
test:
[junit] Running cc.openhome.AbcTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.023 sec
[junit] Running cc.openhome.CalculatorTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.004 sec
BUILD SUCCESSFUL
Total time: 1 second
Buildfile: C:\workspace\example\build.xml
clean:
[delete] Deleting directory C:\workspace\example\bin
prepare:
[mkdir] Created dir: C:\workspace\example\bin
compile:
[javac] C:\workspace\example\build.xml:15: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 3 source files to C:\workspace\example\bin
test:
[junit] Running cc.openhome.AbcTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.023 sec
[junit] Running cc.openhome.CalculatorTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.004 sec
BUILD SUCCESSFUL
Total time: 1 second