您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
junit使用簡明手冊
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/2/18 14:18:29 ] 推薦標(biāo)簽:junit 單元測試工具

2.       使用例子:

import junit.frmework.TestCase;

public class TestSample extends TestCaset{

        public void testMethod1(){

               assertTrue( true);

}

}

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

 

     測試開始 -> setUp -> testXXXX -> tearDown ->測試結(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)的錯誤。產(chǎn)生原因:assert函數(shù)出錯(如assertFalse(true));fail函數(shù)產(chǎn)生(如fail(……))。

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

對于assert、fail等函數(shù)請參見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á)點

}

……

}

 

potected void tearDown(){

       //撤銷初始化……

}

}

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