您的位置:首页 > 其它

使用ant进行junit,并生成report

2014-06-03 17:03 260 查看
一、Junit任务基本概念:
junit是ant的基本任务之一。这个任务运行一个或多个JUNIT测试,并收集以一种或多种格式显示结果。下面是几个junit任务的属性
1、haltonfailure,printsummary分别表示如果测试失败是否中止,是否打印基本信息。
2、fommatter--收集结果数据,一个或多个formatter可以直接在junit,test,或者batchtest下面嵌套使用。有以下三种formatter:
brief:以文本格式提供测试失败的详细内容。
plain:以文本格式提供测试失败的详细内容以及每个测试的运行统计
xml:以xml格式提供扩展的详细内容,包括正在测试时ant的特性,系统输出,以及每个测试用例的系统错误。
<formatter type="xml"/>将会在data目录下为所有的测试用例都创建一个xml文件。
3、test
运行单独的测试用例
<test name=.../>
4、batchtest,同时运行多个测试用例
<formatter type="xml"/>
<batchtest todir="">
<fileset dir="" include=""/>
</batchtest>
测试的输出结果将放到todir。而dir中所有的测试用例都将运行。
xml formatter的默认命名规范为Test-*.xml.
5、syspropertyset,运行junit
test的时候,可以指定syspropertyset,这样在Test*.java文件中可以通过System.getProperty();来获取在构建文件中定义的property的值。例子:
<propertyset id="propertyset1">
<propertyref name=$#@##/>
<propertyref prefix="#%##$"/>
</propertyset>
<junit>
...
<syspropertyset refid="propertyset1"/>
</junit>
6、sysproperty,也可以在junit中定义sysproperty,所定义的property的用法和上面的syspropertyset中的property的用法是一样的。
<sysproperty name="" value=""/>
7、fork="true",让junit运行在独立的jvm中。 ???
二、junitreport任务
可以采用junitreport任务生成html的报告。junitreport任务首先将生成的xml文件整合成一个xml文件,一般命名为TESTS-TestSuites.xml.然后再对xml文件进行转换。其格式如下:
<junitreport>
<fileset dir="${test.data.dir}" includes="Test-*.xml"/>
<report format="frames" todir=""/>
</junitreport>
在上面这个例子里,junitreport任务将整合test.data.dir下面的Test-*.xml文件,并且生成html文件框架.
report表示生成有框架或无框架的javadoc。
三、如何只运行单个测试。

对test和batchtest使用if/unless来实现选择性的运行单个测试或者运行整个测试。
<junit>
<test name=${testcase} if="testcase"/>
<batchset todir="${dest}" unless="testcase">
<fileset .../>
</batchset>
</junit>
if表示只要testcase
这个property存在则会执行test,unless表示将会执行batchset,除非testcase这个property存在。因此如果想要运行单个测试,只需要在命令行中-Dtestcase=...即可。否则将会运行所有的testcases。
四、ant的其他一些数据类型及属性
1、JUNIT---sysproperty,系统属性,定义和property类似。在java文件中可以通过System.getProperty()来获得它的值。

例如:
<junit>
...
<sysproperty key="docs.dir" value="./dest">
</junit>

在java文件中:
System.getProperty("docs.dir");

也可以使用properset定义一个属性集,在junit中引用该属性集,例如:
<property name="property1" value="value1"/>
<property name="property2" value="value2"/>
<propertyset id="myproperty">
<propertyref prefix="property1"/>
<propertyref prefix="property2"/>
</propertyset>
<junit>
...
<syspropertyset refid="myproperty">
</junit>
2、<reference refid="srcid" torefid="tarid"/>

我的理解是定义一个引用的别名,在这里srcid是一个引用,为它定义了一个别名tarid,在当前project用srcid这个引用,如果该project中调用了另一个project的任务,则在另一个project使用tarid这个引用
3、antcall,在一个任务中调用另一个任务。例子:
<target name="exercises">

<property name="directory1" location="d1"/>

<property name="file" location="directory1/a.txt"/>

<echo message="directory = ${directory1}, file=${file}"/>

</target>
<property name="replace1" value="Hello world!!!"/>
<tstamp>

<format property="currenttime" pattern="yyyy-MM-dd'T'HH:mm:ss"/>

</tstamp>

<filterset id="filter.set">

<filter token="welcome" value="${replace1}" />

<filter token="time" value="${currenttime}" />

</filterset>

<target name="exercise3">

<copy todir="d2">

<fileset dir="d1"/>

<filterset refid="filter.set"/>

</copy>

<antcall target="exercises"/>

</target>
如果未定义引用property1,则在此处定义它。
6、depend
<depend srcdir="

destdir="

cache="$"

closure="">

<include name="**/*.java"/>

</depend>
8、 <exec dir="" executable="“

<arg line="-lib ${task.lib.dir} -buildfile ${task.ant.file}"/>

</exec>

试验成功的一个例子:build.xml

<?xml version="1.0"?>

<project name="Cobra" default="junit" basedir=".">

<property environment="env" />

<condition property="ia.home" value="${env.IA_HOME}">

<isset property="env.IA_HOME" />

</condition>

<property name="run.classpath" value="class"></property>

<property name="run.srcpath" value="../src"></property>

<property name="test.xml" value="xml"></property>

<property name="test.report" value="report"></property>

<property name="lib.dir" value="lib"/>

<path id="compile.path">

<fileset dir="${lib.dir}">

<include name="junit.jar" />

<include name="ant.jar" />

<include name="ant-xalan1.jar" />

</fileset>

<fileset dir="${ia.home}">

<include name="IAClasses.zip" />

<include name="resource/services/services.jar" />

<include name="resource/services/ppk/*.jar" />

<include name="resource/ant/ant.jar" />

<include name="resource/log4j-1.2.15.jar" />

</fileset>

</path>

<target name="init">

<delete dir="${run.classpath}"/>

<mkdir dir="${run.classpath}"/>

<delete dir="${test.report}"/>

<mkdir dir="${test.report}"/>

<delete dir="${test.xml}"/>

<mkdir dir="${test.xml}"/>

</target>

<target name="compile" depends="init">

<javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path"/>

</target>

<target name="junit" depends="compile">

<junit printsummary="false">

<classpath path="${run.classpath}">

<path refid="compile.path" />

</classpath>

<formatter type="xml"/>

<batchtest todir="${test.xml}">

<fileset dir="${run.classpath}">

<include name="**/Test*.class"/>

<include name="**/*Test.class"/>

</fileset>

</batchtest>

</junit>

<junitreport todir="${test.xml}">

<fileset dir="${test.xml}">

<include name="TEST-*.xml"/>

</fileset>

<report format="frames" todir="${test.report}"/>

</junitreport>

</target>

</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: