<?xml version="1.0" encoding="UTF-8"?>
<!--
1、build.xml放在程序根目录下,跟src并行
2、新建sorce folder命名为test
3、test目录下与src目录下对应相同目录,放置测试文件
比如:src/com/LoginDao
test/com/LoginDaoTest(测试文件名必须为这种格式)
4、运行cmd,进入程序根目录,输入ant运行就能生成了
5、参考资料:
http://miaoxianjie.iteye.com/blog/1585506
http://ant.apache.org/manual/index.html
default:定义初始运行的target
-->
<project basedir="." default="JUnitAllTests" name="TestJUnit4">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<!-- 配置测试报告生成目录 -->
<property name="junit.output.dir" value="JunitTestResult"/>
<path id="TestJUnit4.classpath">
<pathelement location="bin"/>
</path>
<target name="JUnitAllTests">
<!-- 删除原路径-->
<delete dir="${junit.output.dir}"/>
<!-- 新建路径-->
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<!--
(batchtest)
fork:Run the tests in a separate VM. Overrides value set in <junit>(覆盖原值,非必须).
todir:Directory to write the reports to.(写入目录)
-->
<batchtest fork="yes" todir="${junit.output.dir}">
<!-- 测试文件所在路径 -->
<fileset dir="test">
<!-- 匹配测试文件名 -->
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
<classpath refid="TestJUnit4.classpath"/>
</junit>
<!-- 定位下一个执行target -->
<antcall target="junitreport"/>
</target>
<!-- 配置jUnit报告属性 -->
<target name="junitreport">
<!-- 目标路径 -->
<junitreport todir="${junit.output.dir}">
<!--
fileset:junitreport collects individual xml files generated by the JUnit task using the nested <FileSet> element.
(合并.xml生成报告)
report:would generate a TESTS-TestSuites.xml file in the directory reports and generate the default framed report in the directory
-->
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junit.output.dir}"/>
</junitreport>
</target>
</project>
1、This task has been tested with JUnit 3.0 up to JUnit 3.8.2; it won't work with versions prior to JUnit 3.0. It also works with JUnit 4.0, including "pure" JUnit 4 tests using only annotations and noJUnit4TestAdapter
.
2、
Note: You must have junit.jar
available. You can do one of:
- Put both
junit.jar
andant-junit.jar
inANT_HOME/lib
. - Do not put either in
ANT_HOME/lib
, and instead include their locations in yourCLASSPATH
environment variable. - Add both JARs to your classpath using
-lib
. - Specify the locations of both JARs using a
<classpath>
element in a<taskdef>
in the build file. - Leave
ant-junit.jar
in its default location inANT_HOME/lib
but includejunit.jar
in the<classpath>
passed to<junit>
. (since Ant 1.7)
<?xml version="1.0" encoding="utf-8"?>
<project name="test" default="test" basedir=".">
<!--配置基本属性-->
<property name="src" value="src"/>
<property name="lib" value="lib" />
<property name="base.dir" value="junit"/>
<!-- .class文件存放路径 -->
<property name="build" value="${base.dir}/build"/>
<!-- jar文件存放路径 -->
<property name="dist" value="${base.dir}/dist"/>
<!-- 测试报告存放路径 -->
<property name="classpath" location="${build}"/>
<!--配置测试报告的属性(路径)-->
<property name="report" value="${base.dir}/report"/>
<property name="report.xml" value="${report}/junit/xml"/>
<property name="report.html" value="${report}/junit/html"/>
<!--配置运行时classpath-->
<path id="classpath.run">
<pathelement path="${classpath}"/>
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>
<!--配置测试时classpath-->
<path id="classpath.test">
<path refid="classpath.run"/>
<path location="${dist}/lib/test-${DSTAMP}.jar"/>
</path>
<!--任务初始化-->
<target name="init" >
<tstamp/>
<!-- 删除之前生成的文件 -->
<delete dir="${build}"/>
<delete dir="${report}"/>
<delete dir="${dist}"/>
<!-- 新建路径 -->
<mkdir dir="${build}"/>
</target>
<!--配置编译任务-->
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}">
<classpath refid="classpath.run" />
</javac>
</target>
<!--配置打包任务-->
<target name="dist" depends="compile">
<mkdir dir="${dist}/lib"/>
<jar jarfile="${dist}/lib/test-${DSTAMP}.jar" basedir="${build}"/>
</target>
<!--配置运行任务-->
<target name="run" depends="compile, dist">
<java classname="com.*.*Test">
<classpath>
<path refid="classpath.run"/>
</classpath>
</java>
</target>
<!--配置JUnit测试,打印测试结果-->
<target name="test" depends="compile, dist">
<mkdir dir="${report.xml}"/>
<mkdir dir="${report.html}"/>
<junit printsummary="yes" haltonfailure="no">
<classpath refid="classpath.run"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${report.xml}">
<fileset dir="${src}" includes="**/*Test*.java"/>
</batchtest>
</junit>
<junitreport todir="${report.html}">
<fileset dir="${report.xml}">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="${report.html}"/>
</junitreport>
</target>
</project>