您的位置:首页 > 其它

ant脚本执行junit测试用例

2014-01-07 14:30 411 查看
除了使用java来直接运行junit之外,我们还可以使用ant脚本,结合脚本执行junit用例,并生成测试报告,在进行每日构建等动作时非常有用。

一个完整的例子:

[html]
view plaincopyprint?

<?xml version="1.0"?> <project name="project" default="test"> <property name="build" value="bin"></property> <property name="src" value="src"></property> <property name="src-test" value="src-test"></property> <property name="lib" value="../lib"></property> <property name="testReport" value="testReport"></property> <path id="javac-lib"> <fileset dir="${lib}"> <include name="*.jar" /> </fileset> <!--缺少以下的设置可能出现ClassNotFoundException错误--> <pathelement location="${build}"/> </path> <target name="compile"> <javac destdir="${build}" debug="true" encoding="UTF-8"> <src path="src" /> <classpath refid="javac-lib" /> </javac> </target> <target name="test" depends="compile"> <delete dir="${testReport}" /> <mkdir dir="${testReport}" /> <!--执行JUnit测试用例--> <junit printsummary="yes"> <classpath refid="javac-lib"/> <formatter type="xml"/> <batchtest todir="${testReport}"> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> <!--生成html的测试报告--> <junitreport todir="${testReport}"> <fileset dir="${testReport}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${testReport}"/> </junitreport> <!--删除xml的测试结果--> <delete dir="${testReport}"> <include name="*.xml" /> </delete> </target> </project>

<?xml version="1.0"?>
<project name="project" default="test">
<property name="build" value="bin"></property>
<property name="src" value="src"></property>
<property name="src-test" value="src-test"></property>
<property name="lib" value="../lib"></property>
<property name="testReport" value="testReport"></property>
<path id="javac-lib">
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>

<!--缺少以下的设置可能出现ClassNotFoundException错误-->
<pathelement location="${build}"/>
</path>

<target name="compile">
<javac destdir="${build}" debug="true" encoding="UTF-8">
<src path="src" />
<classpath refid="javac-lib" />
</javac>
</target>

<target name="test" depends="compile">
<delete dir="${testReport}" />
<mkdir dir="${testReport}" />

<!--执行JUnit测试用例-->
<junit printsummary="yes">
<classpath refid="javac-lib"/>
<formatter type="xml"/>
<batchtest todir="${testReport}">
<fileset dir="${build}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>

<!--生成html的测试报告-->
<junitreport todir="${testReport}">
<fileset dir="${testReport}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${testReport}"/>
</junitreport>

<!--删除xml的测试结果-->
<delete dir="${testReport}">
<include name="*.xml" />
</delete>
</target>
</project>


以上实例按几个步骤执行:
1、设置了代码路径、编译环境等

2、执行javac编译target

3、执行junit测试target

其中junit target又按几个步骤执行:

1、初始化报告输出目录

2、执行junit用例,'<junit printsummary="yes">'

3、<formatter type="xml"/>生成xml的测试结果

4、batchtest批量执行用例

5、junitreport根据每个xml用例执行结果生成html格式报告

6、删除每个xml用例结果,如果想看生成xml格式结果,可以把这一步屏蔽

PS:

执行过程中发生ClassNotFoundException错误的解决办法,注意在环境中是否加入了你的编译路径,<pathelement location="${build}"/>参考:http://yusudong.iteye.com/blog/1134915

JUnit Task详细的参数以及各参数的描述可以参考官网的说明:http://ant.apache.org/manual/Tasks/junit.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: