1.将 junit.jar 放在 ANT_HOME\lib 目录下
2.修改 build.xml ,加入如下 内容: 以下是我自己的:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
2009-2-27 下午02:26:07
TestAnt2Junit
description
Administrator
====================================================================== -->
<project name="TestAnt2Junit" default="junit">
<description>
测试ant junit 增量开发
</description>
<property name="run.calsspath" value="bin"/>
<property name="run.srcpath" value="src"/>
<property name="test.srcpath" value="src"/>
<property name="test.report" value="report"/>
<property name="lib.dir" value="lib"/>
<path id="compile.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- =================================
target: junit
================================= -->
<target name="junit" depends="compile" description="单元测试">
<tstamp></tstamp>
<mkdir dir="${test.report}"/>
<mkdir dir="${test.report}/framework-${DSTAMP}-${TSTAMP}"/>
<junit printsummary="true">
<classpath>
<pathelement path="${run.calsspath}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<formatter type="plain"/>
<batchtest fork="yes" todir="${test.report}/framework-${DSTAMP}-${TSTAMP}">
<fileset dir ="${test.srcpath}" >
<include name ="**/*Test.java" />
</fileset >
</batchtest >
</junit >
</target>
<!-- =================================
target: compile
================================= -->
<target name="compile" description="编译">
<javac destdir="${run.calsspath}" srcdir="${run.srcpath}" classpathref="compile.path"/>
<javac destdir="${run.calsspath}" srcdir="${test.srcpath}" classpathref="compile.path"/>
</target>
</project>
ant 提供了两个 target : junit 和 junitreport
运行所有 测试用例 ,并生成 html 格式的报表
下面是别人的:
<property name="report" value="report" />
<target name="junitreport" depends="clean, compile">
<junit printsummary="on"
fork="true"
haltonfailure="false"
failureproperty="tests.failed"
showoutput="true">
<classpath refid="myclasspath"/>
<formatter type="xml"/>
<batchtest todir="${report}">
<fileset dir="${build}">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report}"/>
</junitreport>
<fail if="tests.failed">
---------------------------------------------------------
One or more tests failed, check the report for detail...
---------------------------------------------------------
</fail>
</target>