重新執行Android源碼中的AndroidTests測試程式

摘要:重新執行Android源碼中的AndroidTests測試程式

 
在Android中要進行程式測試,一般有兩種方式;一種是使用junit,這在java中很常被使用到,但在android framework中,單單使用junit沒辨法有更互動的測試。如果想要測試android specified api就得用另一種方式,Instrumentation。Instrumentation在
官方網頁的解釋如下:

Base class for implementing application instrumentation code. When running with instrumentation turned on, this class will be instantiated for you before any of the application code, allowing you to monitor all of the interaction the system has with the application. An Instrumentation implementation is described to the system through an AndroidManifest.xml's <instrumentation> tag.

它可以被理解為一種沒有圖形界面、具有啟動能力、用於監控其他類別的型別(使用Target Package指定)。任何想成為Instrumentation的類別必须繼承android.app.Instrumentation (Ref1, Ref2, Ref3)。當要使用Instrumentation這種方式去測試需要在manifest中加入runner libraryinstrumentation tag 

runner library:
<uses-library android:name="android.test.runner" />
instrumentation tag:
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.android.apis" android:label="Tests for Api Demos."/>

可使用的類別請參考android.test這個package,其中有不同量級的Testcase可以使用,有些可以拿到Context,進而存取Resource、有些可以拿到Activity、有些可以Instrumentation或是使用量級很高的ActivityInstrumentationTestCase2來幫你做不同層次的測試!相關的範例可以參考APIDemo,如果你有下載source code,可以找到更多的測試程式,如framework目錄下就有許多範例可以參考。接下來我以AndroidTests這個suite來進來測試。

Step 0:
下載source code 1.5版並且編譯完後,到out/home/neil/mydroid15/out/target/product/generic/obj/APPS/AndroidTests_intermediates找 package.apk這個檔並安裝到你要執行的裝置上。要拿一個apk檔去測試另一個apk檔時,用指令安裝是最方便的方式。
P.S. 1.1版中,除了上述的找得到apk檔之外,也能在out/target/product/generic/system/app中找到AndroidTests.apk。在1.5中似乎把AndroidTests.apk藏起來了!

Step 1:
使用指令安裝apk (一般的測試其實可以直接使用eclipse來測試,但因為AndroidTests裡使用一些internal api,所以直接import會有些麻煩!)
#adb install package.apk //安裝package.apk

or

#adb devices //如果要指定安裝到某個裝置
emulator-5554   device
HT95EKF02560    device

#adb -s emulator-5554 install package.apk

Step 2:
安裝完後開始測試
#adb shell am instrument -w com.android.unit_tests/android.test.InstrumentationTestRunner
在hero上跑大概十分鐘,模擬器要視電腦效能而論。

P.S. 指令參數
adb shell am instrument [-r] [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>] [-w] <COMPONENT>
e是額外的參數,可指定執行目標的類別或函式
w是component參數:執行目標/執行器。

ex1:
target: com.android_unit_tests
runner: android.test.InstrumentationTestRunner
component: com.android_unit_tests/android.test.InstrumentationTestRunner
$ am instrument -w com.android_unit_tests/android.test.InstrumentationTestRunner

ex2:
TestSuite
$ am instrument -e com.android_unit_tests.AndroidTests -w com.android_unit_tests/android.test.InstrumentationTestRunner
Testcase
$ am instrument -e com.android_unit_tests.BluetoothTest -w com.android_unit_tests/android.test.InstrumentationTestRunner
TestMethod
$ am instrument -e com.android_unit_tests.BluetoothTest#testEnableBluetooth -w com.android_unit_tests/android.test.InstrumentationTestRunner

Ref1: http://speedfirst.spaces.live.com/blog/cns!5D6E8B35D225421F!394.entry
Ref2: http://www.51testing.com/html/87/n-129087.html
Ref3: http://ysl-paradise.blogspot.com/2009/03/testing-android-applications-part-1.html
Ref4: http://mintelong.javaeye.com/blog/460903