第七十一講:Android的單元測試類

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   sp   java   

我們應當努力奮鬥,有所作為。這樣,我們就可以說,我們沒有虛度年華,並有可能在時間的沙灘上留下我們的足跡。——拿破倫一世


本講內容:單元測試類

在Android應用開發過程中進行單元測試,個人在做項目的過程中,覺得單元測試很有必要,以保證我們編寫程式的正確性。


一、單元測試步驟 :

第一步:在AndroidManifest.xml中的 <application></application>裡面添加

<uses-library android:name="android.test.runner" />
第二步:在<application></application>外面添加

<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
<instrumentation android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.example.text2"/>
注釋:其中的android:targetPackage="com.example.text2"指定的包和
               package="com.example.text2">中的package相同即可。

第三步:建立一個單元測試類繼承AndroidTestCase


在Android應用開發中有兩種方式可以進行單元測試,下面分別展示這兩種單元測試的方法:

二、單元測試方式一:應用的工程上進行單元測試



下面是JUnitService類(有個 add方法,計算兩個整型資料的和)

public class JUnitService {public int add(int a,int b) {return a+b;}}

下面是測試包中的MyJnuitTest類(驗證程式結果是否是我們所期望的一致)

public class MyJnuitTest extends AndroidTestCase{public void testAdd(){JUnitService s=new JUnitService();int sum=s.add(1, 2);Assert.assertEquals(3, sum);}}

在AndroidManifest.xml檔案中註冊

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <span style="color:#ff0000;"> <uses-library android:name="android.test.runner" /></span>        <activity            android:name="demo.TestDemoActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".JUnitService"></activity>    </application>    <span style="color:#ff0000;"><uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />        <instrumentation android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.example.text2"/></span></manifest>

開啟eclipse中的outline視窗,其中會顯示單元測試類的所有的方法然後想要測試哪個方法,則在哪個測試方法上右鍵滑鼠,選擇Run As,然後再選擇Android JUnit Test即可,如果是正常的,則會如下:


如果有異常或者錯誤,則會出現如下情況:



三、單元測試方式二:

這種方式更加簡單,這種方式是單獨建立一個單元測試的工程來進行測試。即建立一個 Android Test Project ,然後選擇需要單元測試的項目就好了,通過這種方式進行單元測試的話就不用進行上面的配置,建立這種工程的時候,系統預設已經幫我們配置好了。在上一講中我們已經詳細講解了。


本講就到這裡,Take your time and enjoy it



第七十一講:Android的單元測試類

聯繫我們

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