在使用XP進(jìn)行開發(fā)的過程,unit test是必不可少的環(huán)節(jié)。作為unit test,junit是首選的工具。本文從使用目的、如何使用、以及使用中需要考慮的問題,簡(jiǎn)略描述了junit的基本用法。使用目的 junit是java中書寫unit test的framework,目前一些流行的unit test工具大都都是在junit上擴(kuò)展而來的。目前它的版本是junit3.8.1,可以從www.上下載。 用法1. 基本使用步驟,Junit的使用非常簡(jiǎn)單,它的基本使用步驟: - 創(chuàng)建,從junit.framework.TestCase派生unit test需要的test case - 書寫測(cè)試方法,提供類似于如下函數(shù)簽名的測(cè)試方法: public void testXXXXX(); - 編譯,書寫完test case后,編譯所寫的test case類 - 運(yùn)行,啟動(dòng)junit test runner,來運(yùn)行這個(gè)test case。 Junit提供了2個(gè)基本的test runner:字符界面和圖形界面。啟動(dòng)命令分別如下: 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. setUp與tearDown,這兩個(gè)函數(shù)是junit framework中提供初始化和反初始化每個(gè)測(cè)試方法的。setUp在每個(gè)測(cè)試方法調(diào)用前被調(diào)用,負(fù)責(zé)初始化測(cè)試方法所需要的測(cè)試環(huán)境;tearDown在每個(gè)測(cè)試方法被調(diào)用之后被調(diào)用,負(fù)責(zé)撤銷測(cè)試環(huán)境。它們與測(cè)試方法的關(guān)系可以描述如下:
測(cè)試開始 -> setUp -> testXXXX -> tearDown ->測(cè)試結(jié)束
4. 使用例子: import junit.frmework.TestCase; public class TestSample extends TestCaset{ protected void setUp(){ //初始化…… } public void testMethod1(){ assertTrue( true); } potected void tearDown(){ //撤銷初始化…… } } 5. 區(qū)分fail、exception。 - fail,期望出現(xiàn)的錯(cuò)誤。產(chǎn)生原因:assert函數(shù)出錯(cuò)(如assertFalse(true));fail函數(shù)產(chǎn)生(如fail(……))。 - exception,不期望出現(xiàn)的錯(cuò)誤,屬于unit test程序運(yùn)行時(shí)拋出的異常。它和普通代碼運(yùn)行過程中拋出的runtime異常屬于一種類型。 對(duì)于assert、fail等函數(shù)請(qǐng)參見junit的javadoc。 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.”); //不可能到達(dá) }catch(Exception e){ fail( “Yes, I catch u”); //應(yīng)該到達(dá)點(diǎn) } …… } potected void tearDown(){ //撤銷初始化…… } } 7. 組裝TestSuite,運(yùn)行更多的test。在junit中,Test、TestCase和TestSuite三者組成了composiste pattern。通過組裝自己的TestSuite,可以完成對(duì)添加到這個(gè)TestSuite中的所有的TestCase的調(diào)用。而且這些定義的TestSuite還可以組裝成更大的TestSuite,這樣同時(shí)也方便了對(duì)于不斷增加的TestCase的管理和維護(hù)。 它的另一個(gè)好處就是,可以從這個(gè)TestCase樹的任意一個(gè)節(jié)點(diǎn)(TestSuite或TestCase)開始調(diào)用,來完成這個(gè)節(jié)點(diǎn)以下的所有TestCase的調(diào)用。提高了unit test的靈活性。 8. 使用例子: import junit.framework.Test; import junit.framework.TestSuite; public class TestAll{ public class TestAll{ //定義一個(gè)suite,對(duì)于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ú)的一個(gè)TestCase是一樣的,參見step 1 “運(yùn)行”。 9. 使用Ant junit task。我們除了使用java來直接運(yùn)行junit之外,我們還可以使用junit提供的junit task與ant結(jié)合來運(yùn)行。涉及的幾個(gè)主要的ant task如下: - <junit>,定義一個(gè)junit task - <batchtest>,位于<junit>中,運(yùn)行多個(gè)TestCase - <test>,位于<junit>中,運(yùn)行單個(gè)TestCase - <formatter>,位于<junit>中,定義一個(gè)測(cè)試結(jié)果輸出格式 - <junitreport>,定義一個(gè)junitreport task - <report>,位于<junitreport>中,輸出一個(gè)junit report 具體的語法請(qǐng)參見相關(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的使用并不很難,然而要書寫一個(gè)好的TestCase卻并非易事。一個(gè)不好的TestCase往往是既浪費(fèi)了時(shí)間,也起不了實(shí)際的作用。相反,一個(gè)好的TestCase,不僅可以很好的指出代碼中存在的問題,而且也可以作為代碼更準(zhǔn)確的文檔,同時(shí)還在持續(xù)集成的過程中起非常重要的作用。在此給出書寫TestCase時(shí)需要注意的幾點(diǎn): - 測(cè)試的獨(dú)立性:一次只測(cè)試一個(gè)對(duì)象,方便定位出錯(cuò)的位置。這有2層意思:一個(gè)TestCase,只測(cè)試一個(gè)對(duì)象;一個(gè)TestMethod,只測(cè)試這個(gè)對(duì)象中的一個(gè)方法。 - 給測(cè)試方法一個(gè)合適的名字。 - 在assert函數(shù)中給出失敗的原因,如:assertTrue( “… should be true”, ……),方便查錯(cuò)。在這個(gè)例子中,如果無法通過assertTrue,那么給出的消息將被顯示。在junit中每個(gè)assert函數(shù)都有第一個(gè)參數(shù)是出錯(cuò)時(shí)顯示消息的函數(shù)原型。 - 測(cè)試所有可能引起失敗的地方,如:一個(gè)類中頻繁改動(dòng)的函數(shù)。對(duì)于那些僅僅只含有getter/setter的類,如果是由IDE(如Eclipse)產(chǎn)生的,則可不測(cè);如果是人工寫,那么最好測(cè)試一下。 - 在setUp和tearDown中的代碼不應(yīng)該是與測(cè)試方法相關(guān)的,而應(yīng)該是全局相關(guān)的。如針對(duì)與測(cè)試方法A和B,在setUp和tearDown中的代碼應(yīng)該是A和B都需要的代碼。 - 測(cè)試代碼的組織:相同的包,不同的目錄。這樣,測(cè)試代碼可以訪問被測(cè)試類的protected變量/方法,方便測(cè)試代碼的編寫。放在不同的目錄,則方便了測(cè)試代碼的管理以及代碼的打包和發(fā)布。一個(gè)例子如下: src <=源代碼根目錄 |---com |---mod1 |---class1 junit <=測(cè)試代碼根目錄 |---com |---mod1 |---class1 |
|