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

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(){

       //撤銷初始化……

}

}

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