您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
junit使用手冊
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/1/9 15:51:21 ] 推薦標(biāo)簽:

7.       組裝TestSuite,運(yùn)行更多的test。在junit中,Test、TestCase和TestSuite三者組成了composiste pattern。通過組裝自己的TestSuite,可以完成對添加到這個TestSuite中的所有的TestCase的調(diào)用。而且這些定義的TestSuite還可以組裝成更大的TestSuite,這樣同時(shí)也方便了對于不斷增加的TestCase的管理和維護(hù)。

      它的另一個好處是,可以從這個TestCase樹的任意一個節(jié)點(diǎn)(TestSuite或TestCase)開始調(diào)用,來完成這個節(jié)點(diǎn)以下的所有TestCase的調(diào)用。提高了unit test的靈活性。

8.       使用例子:

import junit.framework.Test;

import junit.framework.TestSuite;

public class TestAll{

public class TestAll{

       //定義一個suite,對于junit的作用可以視為類似于java應(yīng)用程序的main。

   public static Test suite(){

       TestSuite suite = new TestSuite("Running all tests.");

       suite.addTestSuite( TestCase1.class);

       suite.addTestSuite( TestCase2.class);

       return suite;

   }

}

運(yùn)行同運(yùn)行單獨(dú)的一個TestCase是一樣的,參見step 1 “運(yùn)行”。

9.       使用Ant junit task。我們除了使用java來直接運(yùn)行junit之外,我們還可以使用junit提供的junit task與ant結(jié)合來運(yùn)行。涉及的幾個主要的ant task如下:

-          <junit>,定義一個junit task

-          <batchtest>,位于<junit>中,運(yùn)行多個TestCase

-          <test>,位于<junit>中,運(yùn)行單個TestCase

-          <formatter>,位于<junit>中,定義一個測試結(jié)果輸出格式

-          <junitreport>,定義一個junitreport task

-          <report>,位于<junitreport>中,輸出一個junit report

具體的語法請參見相關(guān)文檔。

10.   使用例子:

<junit printsummary="yes" haltonfailure="no">

   <classpath>

       <path refid="classpath"/>

       <pathelement location="${dist.junit}"/>

   </classpath>

  

   <formatter type="brief" usefile="false"/>

   <formatter type="xml"/>

 

   <batchtest todir="${doc.junitReport}">

       <fileset dir="${dist.junit}" includes="**/*Test.class" />

   </batchtest>

</junit>

 

<junitreport todir="${doc.junitReport}">

   <fileset dir="${doc.junitReport}">

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

   </fileset>

   <report format="frames" styledir="${junit.styleDir}" todir="${doc.junitReport}"/>

</junitreport>

檢查表
      junit的使用并不很難,然而要書寫一個好的TestCase卻并非易事。一個不好的TestCase往往是既浪費(fèi)了時(shí)間,也起不了實(shí)際的作用。相反,一個好的TestCase,不僅可以很好的指出代碼中存在的問題,而且也可以作為代碼更準(zhǔn)確的文檔,同時(shí)還在持續(xù)集成的過程中起非常重要的作用。在此給出書寫TestCase時(shí)需要注意的幾點(diǎn):

-          測試的獨(dú)立性:一次只測試一個對象,方便定位出錯的位置。這有2層意思:一個TestCase,只測試一個對象;一個TestMethod,只測試這個對象中的一個方法。

-          給測試方法一個合適的名字。

-          在assert函數(shù)中給出失敗的原因,如:assertTrue( “… should be true”,  ……),方便查錯。在這個例子中,如果無法通過assertTrue,那么給出的消息將被顯示。在junit中每個assert函數(shù)都有第一個參數(shù)是出錯時(shí)顯示消息的函數(shù)原型。

-          測試所有可能引起失敗的地方,如:一個類中頻繁改動的函數(shù)。對于那些僅僅只含有g(shù)etter/setter的類,如果是由IDE(如Eclipse)產(chǎn)生的,則可不測;如果是人工寫,那么好測試一下。

-          在setUp和tearDown中的代碼不應(yīng)該是與測試方法相關(guān)的,而應(yīng)該是全局相關(guān)的。如針對與測試方法A和B,在setUp和tearDown中的代碼應(yīng)該是A和B都需要的代碼。

-          測試代碼的組織:相同的包,不同的目錄。這樣,測試代碼可以訪問被測試類的protected變量/方法,方便測試代碼的編寫。放在不同的目錄,則方便了測試代碼的管理以及代碼的打包和發(fā)布。

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd