• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Eclipse學習4-在Eclipse中使用JUnit進行單元測試(下)

    發布: 2007-7-01 18:47 | 作者: admin | 來源: | 查看: 20次 | 進入軟件測試論壇討論

    領測軟件測試網 使用JUnit測試一個應用程序
    現在已經準備好測試JN_test應用程序。為了測試,還需要使用JUnit Wizard創建一個新的class來擴展JUnit測試用例。要使用此wizard,請在Package Explorer 中的JN_test上單擊右鍵,并且選擇New->Other來打開一個New對話框,如圖所示:



    現在展開Java結點并選擇JUnit,然后再選擇JUnit Test Case,單擊Next按鈕,如圖:







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









    在此對話框中,選擇你想要測試的方法,這樣JUnit Wizard能夠為它們創建存根(stub)。因為我們想要測試allocate,see和get,選擇它們,并點擊Finish按鈕來創建JN_testTest class。如下所示:在JN_testTest class中為allocate,set和get方法都創建了一個方法存根: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() {







































        }







































    }







































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

    JN_test testObject;







































       .

       .

       .

        protected void setUp() throws Exception {

            super.setUp();

            testObject = new JN_test();







































    }

    現在就可以用這個對象來進行測試了。比如,allocate方法是用來創建一個整型數組并返回此數組的,所以在testAllocate中使用assertNotNull測試并確信此數組不為空:

    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方法從數組中取得數值,在testGet中用assertEquals來測試此方法。

    public void testGet() {

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







































        }

    And the set method is supposed to return true if it@#s been successful, so we can test it with assertTrue like this

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

    public void testSet() {

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







































        }

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



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

    public void testAllocate( ) {


     


            assertNotNull(testObject.allocate( ));


     


    }

    看看第一個trace:哦,這個地方遺漏了創建數組,我們修正代碼:

    public int[] allocate()

        {

            array = new int[3];







































            array[0] = 0;

            array[1] = 1;

            array[2] = 2;

            return array;

    }

    現在再運行JN_testTest,現在只有testGet和testSet兩處錯誤,如圖:



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


     


     



     



     



     



     



     



     



     



     



    public void testGet() {

            testObject.allocate();







































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

        }


     


     



     



     



     



     



     



     



     



     



        public void testSet() {

            testObject.allocate();







































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

        }

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



    如上所示,JUnit提供了相當簡單的方式來創建一組標準的測試,我們只需要動幾下鼠標就可以實現。只要測試被創建,你只需要運行你創建的JUnit類。

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




    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>