02_android下單元測試

來源:互聯網
上載者:User

標籤:http   虛擬機器   eric   oid   boa   density   rac   man   class   

Java的單元測試JUnit。

Java程式入口是main方法。一般不在安卓程式入口

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }

做測試。

package com.itheima.test;import android.test.AndroidTestCase;public class MyMathTest extends AndroidTestCase {      public void testAdd(){          MyMath math = new MyMath();          int result = math.add(3, 4);          assertEquals(7, result);//斷言,前面是期望值,後面是實際值      }}

 

這是一個安卓的應用,最終想測試這個方法,必須得把代碼跑到裝置上才行。代碼得跑在ARM/Dalvik虛擬機器才行。所以首先要把代碼部署到裝置上。

[2017-06-13 06:38:14 - SDK Manager] Created AVD ‘android95device‘ based on Android 4.2, Intel Atom (x86) processor,[2017-06-13 06:38:14 - SDK Manager] with the following hardware config:[2017-06-13 06:38:14 - SDK Manager] hw.sdCard=yes[2017-06-13 06:38:14 - SDK Manager] hw.device.manufacturer=Generic[2017-06-13 06:38:14 - SDK Manager] hw.mainKeys=yes[2017-06-13 06:38:14 - SDK Manager] hw.lcd.density=160[2017-06-13 06:38:14 - SDK Manager] hw.accelerometer=yes[2017-06-13 06:38:14 - SDK Manager] hw.dPad=no[2017-06-13 06:38:14 - SDK Manager] hw.device.hash=-1587417588[2017-06-13 06:38:14 - SDK Manager] hw.trackBall=yes[2017-06-13 06:38:14 - SDK Manager] hw.device.name=3.2in QVGA (ADP2)[2017-06-13 06:38:14 - SDK Manager] hw.camera.back=none[2017-06-13 06:38:14 - SDK Manager] hw.sensors.proximity=yes[2017-06-13 06:38:14 - SDK Manager] hw.battery=yes[2017-06-13 06:38:14 - SDK Manager] disk.dataPartition.size=200M[2017-06-13 06:38:14 - SDK Manager] hw.audioInput=yes[2017-06-13 06:38:14 - SDK Manager] hw.sensors.orientation=yes[2017-06-13 06:38:14 - SDK Manager] hw.gps=yes[2017-06-13 06:38:14 - SDK Manager] skin.dynamic=yes[2017-06-13 06:38:14 - SDK Manager] hw.keyboard=yes[2017-06-13 06:38:14 - SDK Manager] vm.heapSize=16[2017-06-13 06:38:14 - SDK Manager] hw.ramSize=512[2017-06-13 07:19:33 - Day03_01_android單元測試] ------------------------------[2017-06-13 07:19:33 - Day03_01_android單元測試] Android Launch![2017-06-13 07:19:33 - Day03_01_android單元測試] adb is running normally.[2017-06-13 07:19:33 - Day03_01_android單元測試] Day03_01_android單元測試 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

必須在資訊清單檔裡面指定一個儀器裝置instrumentation a android.test.InstrumentationTestRunner

 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>
    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <uses-library android:name="android.test.runner"/>

指定了測試要用到的儀器<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>,還有測試所要依賴的庫<uses-library android:name="android.test.runner"/>。

這個代碼一定要在安卓的虛擬機器下執行。PC機上是沒有安卓裝置的。所以一定要把它部署到一個安卓裝置上,才能夠測試安卓項目裡面的代碼。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itheima.test"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <uses-library android:name="android.test.runner"/>        <activity            android:name="com.itheima.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>    </application></manifest>
package com.itheima.test;public class MyMath {       public int add(int i,int j){           return i+j;       }}
package com.itheima.test;import android.test.AndroidTestCase;public class MyMathTest extends AndroidTestCase {      public void testAdd(){          MyMath math = new MyMath();          int result = math.add(3, 4);          assertEquals(7, result);//斷言,前面是期望值,後面是實際值      }}

 

02_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.