Ant下结合Junit构建自动化测试
上一篇 / 下一篇 2012-03-05 13:37:22 / 个人分类:Ant
项目结构:
(1)构建需求
1. 构建自动化;
2. 生成自动化测试报告。
对于被测试源程序和测试脚本,首先要实现编译,生成对应的class文件。然后根据class文件,执行测试脚本进行测试,最后,根据测试结果生成测试报告。
(2)构建过程
targets:
clean: //clean build history
compile-src://compile target src
compile-test://compile testcase
run-test://run testcase
run-report://Generate Test Report base on tests' log
(3)定义构建目录结构
batch//自动化构建目录
build.bat //自动构架的批处理文件
build//类文件的目录结构
build.properties //Ant构建的属性定义文件
src //Ant构建xml文件
test//测试对象源文件保存目录
result//测试脚本类源文件保存目录
classes //编译类保存目录
logs //测试用例执行的log跟踪目录
reports //测试报告输出目录
(4)编写build.property
src.dir=src/zenist/src
test.dir=test/zenist/test
classes.dir=result/classes
logs.dir=result/logs
reports.dir=result/reports
(5)编写build.xml
<?xml version="1.0"?>
<project name="JUnitTest" basedir=".." default="all">
<!-- 定义属性 -->
<property file="${basedir}/build/build.properties" />
<!-- 定义项目classpath变量 -->
<path id="project.classpath">
<pathelement location="${classes.dir}"/>
<fileset dir="${basedir}">
<include name="*.jar" />
</fileset>
</path>
<!-- 清理上次运行结果 -->
<target name="clean">
<delete dir="${classes.dir}" />
<delete dir="${logs.dir}" />
<delete dir="${reports.dir}" />
</target>
<!-- 编译源代码 -->
<target name="compile-src">
<mkdir dir="${classes.dir}" />
<javac destdir="${classes.dir}" debug="true">
<src path="${src.dir}" />
</javac>
</target>
<!-- 编译测试代码 -->
<target name="compile-test" depends="compile-src">
<mkdir dir="${classes.dir}" />
<javac destdir="${classes.dir}" debug="true">
<src path="${test.dir}" />
<classpath refid="project.classpath" />
</javac>
</target>
<!-- 运行测试 -->
<target name="run-test" depends="compile-src, compile-test">
<mkdir dir="${logs.dir}"/>
<junit dir="${basedir}" haltonfailure="false" printsummary="yes"
errorproperty="tests.failed" failureproperty="tests.failed">
<classpath refid="project.classpath" />
<formatter type="plain" usefile="true" />
<formatter type="xml" usefile="true" />
<batchtest fork="yes" todir="${logs.dir}">
<fileset dir="${classes.dir}">
<include name="**/**/TestSample.*" />
</fileset>
</batchtest>
</junit>
<fail if="tests.failed" message="Test(s) failed"/>
</target>
<!-- 生成测试报告 -->
<target name="run-report" depends="run-test"> <mkdir dir="${reports.dir}" />
<junitreport todir="${reports.dir}">
<fileset dir="${logs.dir}">
<include name="Test-*.xml"/>
<include name="Test-*.txt"/>
</fileset>
<report format="frames" todir="${reports.dir}"/>
</junitreport>
</target>
<!-- 默认target all--> <target name="all">
<tstamp/>
<antcall target="clean"/>
<antcall target="run-report"/>
</target>
</project>
TAG: