Android test---JUnit

來源:互聯網
上載者:User

標籤:

JUnit 單元測試

Android的單元測試基礎也是比較簡單,同樣還是測試相加方法是否正確,測試案例還是用1+1?=2來個簡單的Android單元測試;

1..開啟eclipse,建立一個project。單擊功能表列的file-》new-》Java project;project name為testJunit;

2.右鍵新建立的工程testJunit, 選擇new---》Java class,建立一個Java 類;class name 為addclass ;

待測的Java程式如下

public class addclass {    public addclass() {        // TODO Auto-generated constructor stub    }     public int addf(int one,int two){         return one+two;     }}

3.  接下來 ,就可以編寫JUnit 測試案例;

3.1. 右鍵剛剛的工程testJnit,選擇new-》JUnit Test Case ,

3.2 在彈出的new juit test case 對話方塊中選擇 new junit test 3 或new junit test 4;

然後在新的JUnit 測試案例name 輸入測試案例的名稱 如“testaddCase" 點擊finish;(當然可以選擇一次方法了如 setUp(),tearDown())

 

3.3 在testaddCase.java 中編寫test case 如 1+1是否等於2

import junit.framework.TestCase;public class testaddCase extends TestCase {    protected void setUp() throws Exception {        System.out.println("測試準備");    }    protected void tearDown() throws Exception {        System.out.println("測試結束");    }    public void testAdd(){        assertEquals(2,new addclass().addf(1,1));    }}

4. 最後開始執行測試案例;

右鍵工程 選擇run as ----》 JUnit Test

5. 查看測試的結果;

 

 

Android test---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.