duansky'weblog

          統(tǒng)計

          留言簿(3)

          友情鏈接

          閱讀排行榜

          評論排行榜

          JUnit基本教程

          使用目的

          ?????? junitjava中書寫unit testframework,目前一些流行的unit test工具大都都是在junit上擴展而來的。目前它的版本是junit3.8.1,可以從www.junit.org上下載。

          Eclipse中配置junit

          在要使用JUNITproject名上,點擊properties--java build path-libraries, 點擊Add External JARs,JUNIT包點上就行了. 并在需要測試的項目上新建junit test case

          用法

          1.?????? 基本使用步驟,Junit的使用非常簡單,它的基本使用步驟:

          -????????? 創(chuàng)建,從junit.framework.TestCase派生unit test需要的test case

          -????????? 書寫測試方法,提供類似于如下函數(shù)簽名的測試方法:

          public void testXXXXX();

          -????????? 編譯,書寫完test case后,編譯所寫的test case

          -????????? 運行,啟動junit test runner,來運行這個test case。

          Junit提供了2個基本的test runner:字符界面和圖形界面。啟動命令分別如下:

          a 圖形界面:

          java junit.swingui.TestRunner XXXXX

          b 字符界面:

          java junit.textui.TestRunner XXXXX

          2.?????? 使用例子:

          import?junit.frmework.TestCase;?

          public?class?TestSample?extends?TestCaset{?

          ??????????????
          public?void?testMethod1(){?

          ?????????????????????assertTrue(?
          true);?

          ??????????????}
          ?
          }
          ?

          3.?????? setUptearDown,這兩個函數(shù)是junit framework中提供初始化和反初始化每個測試方法的。setUp在每個測試方法調用前被調用,負責初始化測試方法所需要的測試環(huán)境;tearDown在每個測試方法被調用之后被調用,負責撤銷測試環(huán)境。它們與測試方法的關系可以描述如下:

          ???? 測試開始 -> setUp -> testXXXX -> tearDown ->測試結束

          4.?????? 使用例子:

          import?junit.frmework.TestCase;?

          public?class?TestSample?extends?TestCaset{?

          ??????????????
          protected?void?setUp(){?

          ?????????????????????
          //初始化……?

          }
          ?

          ??????????????
          public?void?testMethod1(){?

          ?????????????????????assertTrue(?
          true);?

          }
          ?


          potected?
          void?tearDown(){?

          ???????
          //撤銷初始化……?

          }
          ?}
          ?

          5.?????? 區(qū)分failexception。

          -????????? fail,期望出現(xiàn)的錯誤。產(chǎn)生原因:assert函數(shù)出錯(如assertFalse(true));fail函數(shù)產(chǎn)生(如fail(……))。

          -????????? exception,不期望出現(xiàn)的錯誤,屬于unit test程序運行時拋出的異常。它和普通代碼運行過程中拋出的runtime異常屬于一種類型。

          對于assert、fail等函數(shù)請參見junitjavadoc。

          6.?????? 使用例子:

          import?junit.frmework.TestCase;?

          public?class?TestSample?extends?TestCaset{?

          ??????????????
          protected?void?setUp(){?

          ?????????????????????
          //初始化……?

          }
          ?

          ??????????????
          public?void?testMethod1(){?

          ?????????????????????……?

          ?????????????????????
          try{?

          ????????????????????????????
          boolean?b=?……?

          ????????????????????????????assertTrue(?b);?

          ????????????????????????????
          throw?new?Exception(?“This?is?a?test.”);?

          ????????????????????????????fail(?“Unable?point.”);?????
          //不可能到達?

          ?????????????????????}
          catch(Exception?e){?

          ????????????????????????????fail(?“Yes,?I?
          catch?u”);?//應該到達點?

          }
          ?

          ……?

          }
          ?

          potected?
          void?tearDown(){?

          ???????
          //撤銷初始化……?

          }
          ?
          }
          ?

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

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

          8.?????? 使用例子:

          ?

          import?junit.framework.Test;?

          import?junit.framework.TestSuite;?

          public?class?TestAll{?

          public?class?TestAll{?

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

          ????
          public?static?Test?suite(){?

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

          ????????suite.addTestSuite(?TestCase1.
          class);?

          ????????suite.addTestSuite(?TestCase2.
          class);?

          ????????
          return?suite;?

          ????}
          ?

          }
          ?

          ?

          運行同運行單獨的一個TestCase是一樣的,參見step 1 “運行

          9.?????? 使用Ant junit task。我們除了使用java來直接運行junit之外,我們還可以使用junit提供的junit taskant結合來運行。涉及的幾個主要的ant task如下:

          -????????? <junit>,定義一個junit task

          -????????? <batchtest>,位于<junit>中,運行多個TestCase

          -????????? <test>,位于<junit>中,運行單個TestCase

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

          -????????? <junitreport>,定義一個junitreport task

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

          具體的語法請參見相關文檔。

          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往往是既浪費了時間,也起不了實際的作用。相反,一個好的TestCase,不僅可以很好的指出代碼中存在的問題,而且也可以作為代碼更準確的文檔,同時還在持續(xù)集成的過程中起非常重要的作用。在此給出書寫TestCase時需要注意的幾點:

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

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

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

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

          -????????? setUptearDown中的代碼不應該是與測試方法相關的,而應該是全局相關的。如針對與測試方法AB,在setUptearDown中的代碼應該是AB都需要的代碼。

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

          src?? <=源代碼根目錄

          ?|---com

          ???? |---mod1

          ???????? |---class1

          junit?? <=測試代碼根目錄

          ?|---com

          ???? |---mod1

          ???????? |---class1

          來源:http://autumn200005.blogchina.com/blog/3699946.html

          posted on 2007-11-26 10:59 duansky 閱讀(298) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 滦平县| 如东县| 寿宁县| 九江县| 乡宁县| 道孚县| 惠东县| 观塘区| 临泉县| 铅山县| 张家界市| 行唐县| 武鸣县| 灵川县| 中山市| 梁山县| 淮南市| 临猗县| 平顺县| 渭源县| 台中县| 剑阁县| 南城县| 临猗县| 镇巴县| 望城县| 本溪市| 隆子县| 崇明县| 阜宁县| 子长县| 康平县| 岳池县| 霍州市| 章丘市| 奉节县| 三江| 河西区| 家居| 巴彦县| 梁山县|