android-為應用單元測試

來源:互聯網
上載者:User

標籤:des   android   style   class   blog   c   

  0. 建立一個等待測試的業務類,並添加待測試的方法:

    a. 建立一個名為Junit2的android應用工程;

    b. 在android工程中建立一個名為Demo的類,並添加save和add兩個方法,代碼如下:

package com.example.junit2;public class Demo {    public void save(String username){        String substr = username.substring(6);    }    public int add(int a, int b){        return a+b;    }}

  1. 建立一個Android測試工程,並編寫測試代碼:

    a. 依次點擊File-->New-->Other-->Android Test Project,建立一個名為Junit2Test的Android的測試工程,並選擇第0步建立的Junit2為目標Android工程;

    b. 在Junit2Test工程中添加一個名叫Junit2Test的類,並選擇基類為AndroidTestCase;

    c. 在Junit2Test類中添加如下代碼:

package com.example.junit2.test;import com.example.junit2.Demo;import android.test.AndroidTestCase; import junit.framework.Assert; // for Assertpublic class Junit2Test extends AndroidTestCase {        public void testSave() throws Exception{        Demo test = new Demo();        test.save(null);    }    public void testAdd() throws Exception{        Demo test = new Demo();        int result = test.add(1, 2);        Assert.assertEquals(3, result);    }}

  2. 運行Android測試工程Junit2Test:

  a. 在右側的大綱視圖選擇要啟動並執行測試方法(如testSave()),點擊右鍵,選擇:Run As-->Android JUnit Test,將自動將應用安裝到模擬器並運行;

  b. 查看結果:

  運行完成後,將在左側JUnit視圖給出運行結果,如:

  

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.