您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Eclipse學(xué)習(xí)-用JUnit進(jìn)行單元測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/1/21 16:06:19 ] 推薦標(biāo)簽:

使用JUnit測試一個(gè)應(yīng)用程序
現(xiàn)在已經(jīng)準(zhǔn)備好測試JN_test應(yīng)用程序。為了測試,還需要使用JUnit Wizard創(chuàng)建一個(gè)新的class來擴(kuò)展JUnit測試用例。要使用此wizard,請?jiān)赑ackage Explorer 中的JN_test上單擊右鍵,并且選擇New->Other來打開一個(gè)New對話框,如圖所示:

現(xiàn)在展開Java結(jié)點(diǎn)并選擇JUnit,然后再選擇JUnit Test Case,單擊Next按鈕,如圖:

通常情況下JUnit類命名要和被它測試的類同名,并在其后面添加Test。所以命名為JN_testTest。另外選擇setUp和tearDown方法,這些方法建立和清理在測試用例中的數(shù)據(jù)和(或者)對象(它們在JUnit中的術(shù)語為fixtures)。按照上圖填好后,點(diǎn)擊Next。如圖:

在此對話框中,選擇你想要測試的方法,這樣JUnit Wizard能夠?yàn)樗鼈儎?chuàng)建存根(stub)。因?yàn)槲覀兿胍獪y試allocate,see和get,選擇它們,并點(diǎn)擊Finish按鈕來創(chuàng)建JN_testTest class。如下所示:在JN_testTest class中為allocate,set和get方法都創(chuàng)建了一個(gè)方法存根:testAllocate, testSet, 和 testGet。

package net.csdn.blog;

import junit.framework.TestCase;

public class JN_testTest extends TestCase {

    /*

     * @see TestCase#setUp()

     */

    protected void setUp() throws Exception {

        super.setUp();

    }

    /*

     * @see TestCase#tearDown()

     */

    protected void tearDown() throws Exception {

        super.tearDown();

    }

    public void testAllocate() {

    }

    public void testGet() {

    }

    public void testSet() {

    }

}

下一步是在這些存根中添加代碼,以讓它們來調(diào)用JN_test類中的allocate,set和get方法,這樣能對結(jié)果使用JUnit斷言方法。我們將需要一個(gè)JN_test類的一個(gè)對象來調(diào)用這些方法,將其命名為testObject。要?jiǎng)?chuàng)建testObject使用JUnit代碼中的setUp方法。此方法在JUnit測試開始之前被調(diào)用,這樣我們將從將要測試的JN_test類中創(chuàng)建testObject。

JN_test testObject;

   .

   .

   .

    protected void setUp() throws Exception {

        super.setUp();

        testObject = new JN_test();

}

現(xiàn)在可以用這個(gè)對象來進(jìn)行測試了。比如,allocate方法是用來創(chuàng)建一個(gè)整型數(shù)組并返回此數(shù)組的,所以在testAllocate中使用assertNotNull測試并確信此數(shù)組不為空:

public void testAllocate() {

        assertNotNull(testObject.allocate());

}

The get method is supposed to retrieve a value from the array, so we can test that method using assertEquals with a test value in testGet:

get方法從數(shù)組中取得數(shù)值,在testGet中用assertEquals來測試此方法。

public void testGet() {

        assertEquals(testObject.get(1),1);

    }

And the set method is supposed to return true if it@#s been suclearcase/" target="_blank" >ccessful, so we can test it with assertTrue like this

set方法在成功的時(shí)候返回true,使用assertTrue來測試它。

public void testSet() {

        assertTrue(testObject.set(2,4));

    }

在添加代碼后,選擇Package Explorer 中的JN_testTest,并選擇Run As->JUnit Test 菜單項(xiàng),如圖所示:


上面的圖示說明這里有錯(cuò)誤,在JUnit視圖中,三個(gè)測試都被打上了叉,表示測試失敗,我們先檢查第一個(gè)測試,testAllocate,它測試的是被創(chuàng)建的數(shù)組不能為null:

public void testAllocate( ) {

        assertNotNull(testObject.allocate( ));


}

看看第一個(gè)trace:哦,這個(gè)地方遺漏了創(chuàng)建數(shù)組,我們修正代碼:

public int[] allocate()

    {

        array = new int[3];

        array[0] = 0;

        array[1] = 1;

        array[2] = 2;

        return array;

}

現(xiàn)在再運(yùn)行JN_testTest,現(xiàn)在只有testGet和testSet兩處錯(cuò)誤,如圖:

testGet和testSet出了什么問題?這里的問題是所有的JUnit測試都是單獨(dú)運(yùn)行的。也是說盡管第一個(gè)測試testAllocate調(diào)用了allocate方法,但是它并沒有被接下來的兩個(gè)測試testGet和testSet調(diào)用。所以為了給這兩個(gè)測試初始化其數(shù)組,必須在testGet和testSet中都調(diào)用allocate方法。添加以下代碼:

public void testGet() {

        testObject.allocate();

        assertEquals(testObject.get(1),1);

    }

    public void testSet() {

        testObject.allocate();

        assertTrue(testObject.set(2,4));

    }

現(xiàn)在運(yùn)行測試,全部都通過。JUnit視圖中頂部的菜單欄為綠色,如圖所示:


如上所示,JUnit提供了相當(dāng)簡單的方式來創(chuàng)建一組標(biāo)準(zhǔn)的測試,我們只需要?jiǎng)訋紫率髽?biāo)可以實(shí)現(xiàn)。只要測試被創(chuàng)建,你只需要運(yùn)行你創(chuàng)建的JUnit類。

然而,需要注意的是JUnit只是用一組測試來檢驗(yàn)兼容性,如果你的代碼中存在問題,或者你不知道怎么辦,這時(shí)你需要進(jìn)行debug。(全文完)

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