<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    littleQ

    終于明白曾經他們失落的目光,當年的你們是否一樣;間有懈怠或有頹放,難知多久方能補上;今起,不再彷徨!

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      33 Posts :: 0 Stories :: 60 Comments :: 0 Trackbacks

    前段時間做的一個升級包到線上后出了個bug,原因是因為做測試的時候沒有完全覆蓋到。痛定思痛,決定使用Clover來提高代碼的單元測試覆蓋率。

    開發的IDE是使用的Eclipse3.4,這里主要說一下在Eclipse中如何使用Clover,也是剛使用不久,寫在這里和大家探討一下。

    http://www.cenqua.com/download.jspa下載Clover,還需要申請一個license,否則無法使用。我下載了30天試用版。

    下載后把Clover壓縮包里有兩個文件夾,features和plugins。我試著用links的方式安裝Clover,不幸的失敗了,不知道是不是我哪里配錯了,只好直接把兩個文件夾解壓到ECLIPSE_HOME覆蓋。

    打開Eclipse,從Window->Show view->other,可以看到Clover的信息:

     

    還需要導入clover.license。從Preferences->Clover->License進入:

    使用load,導入clover.license。到這里,Clover已經安裝完成,開始進入我們的單元測試。

     

    我新建了一個project,結構如下:

     

    Sample.java和SampleTest.java內容分別是:

    package com.lyoe.sample;

    public class Sample 
    {
        
    public Integer add(Integer a, Integer b) 
    {
            
    if (a == null || b == null
    {
                
    return null
    ;
            }

            Integer sum 
    = new Integer(a.intValue() + b.intValue());
            
    return
     sum;
        }

    }


     

    package com.lyoe.sample;

    import
     com.lyoe.sample.Sample;
    import
     junit.framework.TestCase;

    public class SampleTest extends TestCase 
    {

        
    protected void setUp() throws Exception 
    {
            
    super
    .setUp();
        }


        
    public void testAdd() {
            Sample sample 
    = new
     Sample();
            Integer a 
    = new Integer(1
    );
            Integer b 
    = new Integer(2
    );
            Integer c 
    =
     sample.add(a, b);
            assertNotNull(c);
        }


        
    protected void tearDown() throws Exception {
            
    super
    .tearDown();
        }


    }

     

     右擊JunitInAction工程,選擇Properties->Clover。上面有一個Enable Clover in this project,勾上,apply->OK.

     

    從Window->Show view->other進入,找到Coverage Explorer,打開,看到Clover選項的相關信息:

    點擊紅框內的按鈕,會彈出重新構建工程的提示,點擊"是"即可。

     

    在Coverage Explorer界面下,我們會看到:



     

     

    右擊SampleTest.java,選擇Run as->JUnit Test,結果如下:



     

    可以看到Sample.java的覆蓋率為71.4%.

     

    Clover還可以生成三種測試報告:PDF/HTML/XML。按鈕可以在Coverage Explorer那排的選項按鈕里找到(run new report)。html的測試報告見附件。

     

    到這里,已經算是能在Eclipse中使用Clover的基本功能了,以后慢慢探索,歡迎大家扔磚~

     

    posted on 2008-10-10 19:28 littleQ 閱讀(2098) 評論(1)  編輯  收藏

    Feedback

    # re: 單元測試覆蓋率-使用Clover 2008-10-11 13:19 Jack.Wang
    不錯,確實不錯,我也試用一下!  回復  更多評論
      


    只有注冊用戶登錄后才能發表評論。


    網站導航: