您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > junit
JUnit介紹及其快速使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/19 15:03:46 ] 推薦標(biāo)簽:

  JUnit 是 Java 社區(qū)中知名度高的單元測(cè)試工具。它誕生于 1997 年,由 Erich Gamma 和 Kent Beck 共同開發(fā)完成。其中 Erich Gamma 是經(jīng)典著作《設(shè)計(jì)模式:可復(fù)用面向?qū)ο筌浖幕A(chǔ)》一書的作者之一,并在 Eclipse 中有很大的貢獻(xiàn);Kent Beck 則是一位極限編程(XP)方面的專家和先驅(qū)。

  麻雀雖小,五臟俱全。JUnit 設(shè)計(jì)的非常小巧,但是功能卻非常強(qiáng)大。Martin Fowler 如此評(píng)價(jià) JUnit:在軟件開發(fā)領(lǐng)域,從來(lái)沒(méi)有如此少的代碼起到了如此重要的作用。它大大簡(jiǎn)化了開發(fā)人員執(zhí)行單元測(cè)試的難度,特別是 JUnit 4 使用 Java 5 中的注解(annotation)使測(cè)試變得更加簡(jiǎn)單。

  從推出到現(xiàn)在,JUnit3.8.1和JUnit4的工作原理和使用區(qū)別還是比較大的,下面首先用一段代碼來(lái)演示JUnit3.8.1的快速使用,以便熟悉JUnit的原理
1.首先,我們?cè)贓clipse的項(xiàng)目中創(chuàng)建一個(gè)待測(cè)試的類Hello.java,代碼如下:
public class Hello {

    public int abs(int num)
    {
        return num>0?num:-num;
    }
    public double division(int a,int b)
    {
        return a/b;
    }
}

  2.右擊該類,選擇 新建->JUnit測(cè)試用例,選擇JUnit3.8.1,setUp和tearDown方法,點(diǎn)擊下一步,選擇需要測(cè)試的方法,JUnit會(huì)自動(dòng)生成測(cè)試的代碼框架,手動(dòng)添加自己的測(cè)試代碼后如下:

 
import junit.framework.TestCase;

public class HelloTest extends TestCase {

    private Hello hello;
    public HelloTest()
    {
        super();
        System.out.println("a new test instance...");
    }
    //測(cè)試前JUnit會(huì)調(diào)用setUp()建立和初始化測(cè)試環(huán)境
    protected void setUp() throws Exception {
        super.setUp();                                    //注意:在Junit3.8.1中這里要調(diào)用父類的setUp()
        hello=new Hello();
        System.out.println("call before test...");
    }
    //測(cè)試完成后JUnit會(huì)調(diào)用tearDown()清理資源,如釋放打開的文件,關(guān)閉數(shù)據(jù)庫(kù)連接等等
    protected void tearDown() throws Exception {
        super.tearDown();                                //注意:在Junit3.8.1中這里要調(diào)用父類的tearDown()
        System.out.println("call after test...");
    }

    //測(cè)試Hello類中的abs函數(shù)
    public void testAbs() {
        System.out.println("test the method abs()");
        assertEquals(16, hello.abs(16));
        assertEquals(11, hello.abs(-10));//在這里,會(huì)出現(xiàn)故障,應(yīng)該把左邊的參數(shù)改為10
        assertEquals(0, hello.abs(0));
       
    }

    //測(cè)試Hello類中的division函數(shù)
    public void testDivision() {
        System.out.println("test the method division()");
        assertEquals(3D, hello.division(6, 2));
        assertEquals(6D, hello.division(6, 1));
        assertEquals(0D, hello.division(6, 0));//在這里,會(huì)出現(xiàn)錯(cuò)誤,java.lang.ArithmeticException: /by zero
       
    }

}

  3.運(yùn)行該測(cè)試類,輸出如下:

a new test instance...
a new test instance...
call before test...
test the method abs()
call after test...

call before test...
test the method division()
call after test...
  從上面的輸出結(jié)果中,可以看出JUnit大概會(huì)生成如下的測(cè)試代碼:
try {
    HelloTest test = new HelloTest();     // 建立測(cè)試類實(shí)例
    test.setUp();             // 初始化測(cè)試環(huán)境
    test.testAbs();             // 測(cè)試abs方法
    test.tearDown();             // 清理資源
}
catch(Exception e){}

try {
    HelloTest test = new HelloTest();     // 建立測(cè)試類實(shí)例
    test.setUp();             // 初始化測(cè)試環(huán)境
    test.testDivision();         // 測(cè)試division方法
    test.tearDown();             // 清理資源
}
catch(Exception e){}

  所以,每測(cè)試一個(gè)方法,JUnit會(huì)創(chuàng)建一個(gè)xxxTest實(shí)例,如上面分別生成了兩個(gè)HelloTest實(shí)例來(lái)分別測(cè)試abs和division方法。

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