Ant运行Junit

 http://blog.sina.com.cn/s/blog_3d21e545010006op.html

 

一、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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值