ANT使用分享(3)----集成junit自動化測試

一、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>

原文:http://www.blogjava.net/joaquin25/articles/194801.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章