編輯:關於 Android 程式設計
要在 Android 上以 TestSuite 批次執行測試,需要自訂一個繼承自 InstrumentationTestRunner 的 Runner,並在清單檔中註冊。以下記錄完整做法。
public class StartTest extends InstrumentationTestRunner {
public TestSuite getAllTests() {
TestSuite suite = new InstrumentationTestSuite(this);
suite.addTestSuite(TestRunner.class);
return suite;
}
}
這裡有三個重點:
1. 類別必須繼承 InstrumentationTestRunner。
2. 方法名稱一定要是 getAllTests(),這是父類的虛方法——一開始沒注意到這點,走了不少冤枉路,特別記下來提醒。
3. 最後要把組好的 TestSuite 回傳。
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.mobile.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="5" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.mobile" />
<instrumentation
android:targetPackage="com.mobile"
android:name="com.mobile.test.StartTest" />
<!-- 新增的 instrumentation:targetPackage 為被測套件,name 為 TestSuite 的套件.類別 -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
在 Eclipse 中右鍵選擇 Run configurations...,勾選「Run all tests in the selected project, or package」,選好測試專案,Instrumentation runner 填你在清單檔設定的 com.mobile.test.StartTest,按下 Run 即可。
也可以用命令列執行:
am instrument -w com.mobile.test/.StartTest
補充一個小技巧:adb shell pm list instrumentation 可以列出裝置上所有已安裝的 instrumentation。
CollapsingToolbarLayout 完全解析:打造折疊式 Toolbar
簡介 在各種應用中,你大概見過這樣的效果:Toolbar 一開始是透明的,襯著一張背景大圖與大標題;隨著頁面向下滑動,標題逐漸縮小並收攏到 Toolbar 上,而背景圖片
OpenCV 學習筆記(六):在 Android 上開啟相機並取得預覽畫面
前面幾篇已經完成了 Android 開發環境的建置,也找到繞過 OpenCV Manager 的方法。這一篇開始動手做一個完整的小程式,最終目標是這樣: 辨識指定圖片,
Android 並發編程(一):程序的啟動與終結
Android 是一個多用戶、多任務的系統,允許眾多 App 同時運行,並在彼此之間快速切換。這一切能力的基礎,是 Linux 內核提供的進程機制。 Linux 進程
Android 如何取得初始狀態下 View 的寬高
在自訂 View 或下拉刷新等情境中,常常需要在初始化階段取得 View 的寬高。然而,在 View 尚未完成 measure 與 layout 之前,getHeight