Android 單元測試

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   c   

我在學習Android的時候最頭特的就是 測試! 虛擬機器慢有不舒服,真機也是覺得很慢,因為要頻繁的安裝應用,所以Android 單元測試的使用就顯得尤為重要了。

想實現簡單的單元測試不是很難,只要幾步就可以完成了:

 

首先要在資訊清單檔(AndroidManifest.xml)下進行如下聲明:

  <!-- 測試環境的指令 放在application節點的外面-->     <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>
<!-- 測試環境的指令
放在application節點的裡面與activity同級--><uses-library android:name="android.test.runner"/>
可參照如下聲明:
 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.tai.mylistview" 4     android:versionCode="1" 5     android:versionName="1.0" > 6     <uses-sdk 7         android:minSdkVersion="8" 8         android:targetSdkVersion="14" /> 9     <uses-permission android:name="android.permission.INTERNET"/>10        <!-- 測試環境的指令 -->11     <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>12     13     <application14         android:allowBackup="true"15         android:icon="@drawable/ic_launcher"16         android:label="@string/app_name"17         android:theme="@style/AppTheme" >18     <!-- 測試環境的指令 -->19     <uses-library android:name="android.test.runner"/>20         <activity21             android:name="com.tai.mylistview.MainActivity"22             android:label="@string/app_name" >23             <intent-filter>24                 <action android:name="android.intent.action.MAIN" />25 26                 <category android:name="android.intent.category.LAUNCHER" />27             </intent-filter>28         </activity>29         <activity android:name="com.tai.mylistview.SimpleAdapter_Act"></activity>30         <activity android:name="com.tai.mylistview.CheckImage"></activity>31         <activity android:name="com.tai.mylistview.DataBase"></activity>32         33         34      <provider 35          android:name="com.tai.provider.PersonDBProvider"36          android:authorities="com.tai.mylistview.personProvider"/>37     </application>38 39 </manifest>
View Code

再就是聲明一個類,這個類要繼承自AndroidTestCase,剩下的就喝Junit是一樣的。

下面這就是對上篇中的資料庫操作的測試方法:

 1 package com.tai.test; 2  3 import com.person.dao.PersonDao; 4 import com.person.dao.PersonDao2; 5 import com.tai.db.PersonDBOpenHelper; 6  7 import android.test.AndroidTestCase; 8  9 public class Test extends AndroidTestCase {10 11     public void testCreateDb() throws Exception12     {13         //getContext() 是android 測試類別 androidTestCase 提供的方便得到測試 內容相關的API14         PersonDBOpenHelper helper = new PersonDBOpenHelper(getContext());15         helper.getReadableDatabase();16     }17     18     private PersonDao2 dao;19     //當測試架構準備好的時候才會執行  一般都坐初始化操作20     @Override21     protected void setUp() throws Exception {22         super.setUp();23         dao = new PersonDao2(getContext());24     }25     //當測試方法執行完之後執行的方法  一般都做些擦屁股的操作26     @Override27     protected void tearDown() throws Exception {28         super.tearDown();29     }30     31     public void testAdd() throws Exception32     {33         boolean result = dao.add("張三","999");34         assertEquals(true, result);35     }36     37     public void testFind() throws Exception38     {39         boolean result = dao.find("張三");40         assertEquals(true, result);41     }42     43     public void testUpdate() throws Exception44     {45         boolean result = dao.update("110","張三");46         assertEquals(true, result);47     }48     public void testDelete() throws Exception49     {50         boolean result = dao.delete("張三");51         assertEquals(true, result);52     }53 }
test

 

下面這兩個方法很實用。

//當測試架構準備好的時候才會執行 一般都坐初始化操作
@Override
protected void setUp() throws Exception {
super.setUp();
dao = new PersonDao2(getContext());
}
//當測試方法執行完之後執行的方法 一般都做些擦屁股的操作
@Override
protected void tearDown() throws Exception {
super.tearDown();
}

 

記得再寫測試方法的時候想單元架構拋出異常!!

OK簡單的測試就這樣可以用了。

 

還有最懶的一個方法  直接產生一個測試工程!

之後就可以直接寫測試類別  測試方法!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.