使用ant 跑junit test 生成 report

来源:互联网 发布:淘宝借贷怎么还 编辑:程序博客网 时间:2024/06/05 04:50
一、Junit任务基本概念:

junit是ant的基本任务之一。这个任务运行一个或多个JUNIT测试,并收集以一种或多种格式显示结果。下面是几个junit任务的属性

1、haltonfailure,printsummary分别表示如果测试失败是否中止,是否打印基本信息。
2、fommatter--收集结果数据,一个或多个formatter可以直接在junit,test,或者batchtest下面嵌套使用。有以下三种formatter:
 
  brief:以文本格式提供测试失败的详细内容。
 
 plain:以文本格式提供测试失败的详细内容以及每个测试的运行统计
 
 xml:以xml格式提供扩展的详细内容,包括正在测试时ant的特性,系统输出,以及每个测试用例的系统错误。 
 
  <formattertype="xml"/>将会在data目录下为所有的测试用例都创建一个xml文件。
3、test
 
运行单独的测试用例
 
<testname=.../>
4、batchtest,同时运行多个测试用例
 
<formattertype="xml"/>
 
<batchtesttodir="">
 
  <fileset dir="" include=""/>
 
</batchtest>
 
测试的输出结果将放到todir。而dir中所有的测试用例都将运行。
 
xml formatter的默认命名规范为Test-*.xml.
5、syspropertyset,运行junittest的时候,可以指定syspropertyset,这样在Test*.java文件中可以通过System.getProperty();来获取在构建文件中定义的property的值。例子:

 
  <propertysetid="propertyset1">
 
   <propertyref name=$#@##/>
 
   <propertyref prefix="#%##$"/>
 
 </propertyset>
 
 <junit>
 
   ...
 
  <syspropertysetrefid="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">

 
   <propertyrefprefix="property1"/>

 
   <propertyrefprefix="property2"/>

 
  </propertyset>

 
  <junit>

 
   ...

 
   <syspropertysetrefid="myproperty">

 
  </junit>

 
2、<reference refid="srcid"torefid="tarid"/>

 
  我的理解是定义一个引用的别名,在这里srcid是一个引用,为它定义了一个别名tarid,在当前project用srcid这个引用,如果该project中调用了另一个project的任务,则在另一个project使用tarid这个引用

 
3、antcall,在一个任务中调用另一个任务。例子:

 
  <targetname="exercises">
<property name="directory1"location="d1"/>
<property name="file"location="directory1/a.txt"/>
<echo message="directory = ${directory1},file=${file}"/>
</target>

<property name="replace1" value="Helloworld!!!"/>

<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">
 
<copytodir="d2">
 
<filesetdir="d1"/>
 
<filtersetrefid="filter.set"/>
 
</copy>
 
<antcalltarget="exercises"/>
</target>

 
如果未定义引用property1,则在此处定义它。

6、depend
 
  <dependsrcdir="
 
              destdir="
 
              cache=""
 
              closure="">
 
          <include name="**/*.java"/>
 
      </depend>

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



六、junit4和ant:
a
nt集成的 junit任务当前仅仅支持JUnit 3.8风格测试, 所以必须用一个JUnit 4TestAdapter来包装你的JUnit 4测试;

fork=true必须。
junit printsummary="yes"
fork="true"haltonfailure="yes"
 
                     

Here follows my JUnit4 test class:

public class TestLogging {

 
      @Test   public voidaSimpleTestMethod() { ... }

 
      @Before public void setUp() throws Exception { .. }

 
      public staticjunit.framework.Test suite() {
               return new JUnit4TestAdapter(TestLogging.class);
       }
}

后续心得:
如果 需要运行时需要环境变量,可以通过下列方法添加:
<junit xxx>
<env key="LD_LIBRARY_PATH" value="${basedir}/path"/>
</junit>

引用地址:
http://www.51testing.com/?uid-213912-action-viewspace-itemid-235623
http://www.jguru.com/forums/view.jsp?EID=1290268
原文出处: http://blog.sina.com.cn/s/blog_56d8ea900100zpgr.html

原创粉丝点击