qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Android學習筆記之如何對應用進行單元測試

          開發環境:
            Win XP + eclipse-jee-helios(版本號3.6) + ADT(版本10.0.1) + Android SDK(版本10);
            模擬器及真機測試環境:Android2.2
            在Android軟件的開發過程中,可以使用Junit測試框架。在Junit中可以得到組件,可以模擬發送事件和測試程序處理的正確性。
            第一步:在新建項目中,創建待測試的業務類,在cn.hao.service包中,代碼如下:
          package cn.hao.service;
          //業務類,待測試的兩個方法
          public class PersonaService {
          public void save(String username){
          String sub = username.substring(6);
          }
          public int add(int a,int b){
          return a+b;
          }
          }
            說明:對于save()方法,如果參數為null,那么這個方法會發生錯誤;對add()方法,我們測試相加返回的相加結果是否正確。
            第二步:為應用引進單元測試環境
            在AndroidManifest.xml中加入如下代碼:
            <uses-library android:name="android.test.runner"/>
            <instrumentation android:name="android.test.instrumentationTestRunner"
            android:targetPackage="cn.hao.JunitTest" android:label="Test for My App" />
            引入的位置如下:
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="cn.hao.JunitTest"
          android:versionCode="1"
          android:versionName="1.0">
          <uses-sdk android:minSdkVersion="8" />
          <application android:icon="@drawable/icon" android:label="@string/app_name">
          <activity android:name="cn.hao.test.MainActivity"
          android:label="@string/app_name">
          <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
          </activity>
          <uses-library android:name="android.test.runner"/>
          </application>
          <instrumentation android:name="android.test.instrumentationTestRunner"
          android:targetPackage="cn.hao.JunitTest" android:label="App Test"
          />
          </manifest>
            說明:在項目中使用單元測試,就是檢查程序及處理結果的正確性。
           第三步:新建一個類,測試業務類,代碼如下:
          package cn.hao.junit;
          import junit.framework.Assert;
          import cn.hao.service.PersonaService;
          import android.test.AndroidTestCase;
          public class PersonServiceTest extends AndroidTestCase {
          public void testSave() throws Exception {
          PersonaService service = new PersonaService();//new出測試對象
          service.save(null);
          }
          public void testAdd() throws Exception {
          PersonaService service = new PersonaService();
          int actual = service.add(1, 2);
          Assert.assertEquals(3, actual);
          }
          }
           
           注意:該類需繼承單元測試框架父類android.test.AndroidTestCase類,測試方法最好是拋出異常給測試框架。方法Assert.assertEquals(3, actual)中參數3是期望(理論上)返回的果,actual是實際上返回的結果。
            第四步:運行測試類
            在大綱OutLine視圖中,右擊測試方法->Run As->Android Junit Test,會將項目自動部署到模擬器上,測試的結果會以顏色的形式顯示,綠色表示方法正確,否則該方法不正確,Eclipse會給出詳細的說明,根據幫助文檔可以查看相應的錯誤信息。
            如測試上述testSave()方法時,會給出如下提示:
            當然,save()從第六位開始取子字符串,但是該方法現在的參數為null,會發生空指針異常。

          posted on 2014-09-19 09:55 順其自然EVO 閱讀(291) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 榆中县| 衡水市| 沙雅县| 吴忠市| 凤冈县| 阳信县| 阿拉善盟| 久治县| 元氏县| 九江县| 洛隆县| 榆树市| 灌云县| 久治县| 石屏县| 定边县| 剑河县| 双流县| 民权县| 沂水县| 金坛市| 芜湖市| 兰西县| 伊宁市| 平武县| 永吉县| 沭阳县| 吉木乃县| 雷州市| 淮南市| 贺州市| 武鸣县| 鹤岗市| 澄迈县| 深泽县| 杭州市| 龙海市| 新乡县| 宣恩县| 白银市| 海淀区|