您的位置:首页 > 其它

ant学习笔记(三)ant整合junit做测试并生成测试报告

2013-01-11 13:26 615 查看
build.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
1.创建属性
2.编译源文件
3.编译test文件
4.运行单元测试
5.生成单元测试报告
-->
<!-- 如果文件夹换名,应先执行删除文件夹deleteDir任务,在更换build文件名 -->
<project name="junit_test" default="run_test">

<!--1.使用属性定义相应的路径时,一定要使用location,会转换系统的分隔符-->
<property name="src.dir" location="src"></property>
<property name="test.src.dir" location="test"></property>
<property name="build.dir" location="build"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.test.dir" location="${build.dir}/test"></property>
<property name="build.test.classes" location="${build.test.dir}/classes"></property>
<property name="build.test.report" location="${build.test.dir}/report"></property>
<property name="lib.dir" location="lib"></property>
<property name="run.test.class" value="**/Test*"></property>
<!-- 路径不建议放在properties中定义;如果属性太多可以在外部文件中定义
<property file="build.properties"></property>
-->
<!--把环境变量中的参数导出到env这个变量中进行使用
<property environment="env"></property>
-->

<!--最佳实践:在项目中增加一个lib文件夹;使用junit本身的jar包,然后添加到编译环境之中-->
<path id="complie_path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile_test_path">
<path refid="complie_path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run_test_path">
<path refid="compile_test_path"></path>
<pathelement location="${build.test.classes}"/>
</path>

<target name="clean">
<echo>进行项目的清理工作</echo>
<delete dir="${build.dir}"></delete>
</target>
<target name="init" depends="clean">
<echo>进行项目的初始化</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target>

<target name="compile" depends="init">
<echo>编译源文件</echo>
<javac failonerror="true" srcdir="${src.dir}" destdir="${build.classes}" classpathref="compile_test_path"></javac>
</target>

<target name="compile_test" depends="compile">
<echo>编译测试文件</echo>
<javac failonerror="true" includeantruntime="true" srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile_test_path"></javac>
</target>

<target name="run_test" depends="compile_test">
<echo>运行单元测试</echo>
<!--4.
设置信息直接显示:printsummary="true"
出错后不再向下执行:haltonfailure="true"
-->
<junit printsummary="false" haltonfailure="false">
<!--1.设置run路径-->
<classpath refid="run_test_path"></classpath>
<!--2.设置显示错误信息的格式-->
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<!--3.设置类路径-->
<!--3.1单文件测试<test name="${run.test.class}"></test>-->
<!--
3.2批量测试
todir设置报告文件路径
-->
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
</batchtest>
<!--3.设置显示错误信息-->
</junit>
<!--
5.设置junit格式化的的文件夹
并进行转换
-->
<junitreport todir="${build.test.report}">
<fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset>
<report format="frames" todir="${build.test.report}/html"/>
</junitreport>
</target>

<target name="end" depends="compile_test">
<echo>整个过程结束</echo>
</target>
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: