1 Instrument是什麼?android
instrument爲am命令的一個子命令。用於啓動一個Instrumentation測試。首先鏈接手機或者模擬器,經過adb shell命令,進入shell層進行操做。shell
2 命令格式及參數解讀(來自官網)框架
格式:instrument [options] component性能
目標 component是表單 test_package/runner_class,
在UiAutomator2.0中,目標 component爲:測試包名/android.support.test.runner.AndroidJUnitRunner,
(即運行器固定:AndroidJUnitRunner類是一個JUnit測試運行器,容許運行JUnit 3或JUnit 4測試類在 Android 設備上,包括那些使用Espresso和UI Automator框架。)測試
各項參數:動畫
-r
:以原始形式輸出測試結果;該選項一般是在性能測試時與[-e perf true]
一塊兒使用。
-e name value
:提供了以鍵值對形式存在的過濾器和參數。例如:-e testFile <filePath>(運行文件中指定的用例);-e package <packageName>(運行這個包中的全部用例)…… 有十幾種。
-p file
:將分析數據寫入 file
。
-w
:測試運行器須要使用此選項。-w <test_package_name>/<runner_class> :<test_package_name>和<runner_class>在測試工程的AndroidManifest.xml中查找,做用是保持adb shell打開直至測試完成。
--no-window-animation
:運行時關閉窗口動畫。
--user user_id | current
:指定儀器在哪一個用戶中運行;若是未指定,則在當前用戶中運行。
3 怎麼運行手機中現有的instrumentation, 並輸出詳細結果,同時將profiling性能數據寫到本地文件中?ui
- 先列出手機中已安裝的instrumentation:adb shell pm list instrumentation
- adb shell am instrument XXX
4 命令的具體使用,好比com.le.tcauto.uitest.test是包含全部測試代碼的應用的包名:(來自:http://blog.csdn.net/swordgirl2011/article/details/50881390).net
- 運行全部的用例: adb shell am instrument -w com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行一個類中的全部用例:adb shell am instrument -w -r -e class com.letv.leview.setproxy com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行類中的某個方法:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy#testDemo com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行多個類的全部用例:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy,com.letv.leview.resetdate com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行全部測試用例除了指定的類:adb shell am instrument -w -r -e notClass com.letv.leview.setproxy com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行全部測試除了指定的用例:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy#testDemo com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 運行文件中的所列的用例:adb shell am instrument -w -e testFile /sdcard/tmp/testFile.txt com.android.foo/com.android.test.runner.AndroidJUnitRunner 文件制定的 格式爲:com.android.foo.FooClaseName#testMethodName
- 運行指定測試切片的用例:adb shell am instrument -w -e numShards 4 -e shardIndex 1 com.android.foo/android.support.test.runner.AndroidJUnitRunner
- 運行指定註解的測試用例:adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner。若是使用多個選項,則運行的用例爲二者的交集,好比:「-e size large -e annotation com.android.foo.MyAnnotation」,將只運行同時含LargeTest和MyAnnotation註解的用例。
- 運行沒有指定註解的用例:adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner,指定多個註解,用「,」隔開,e.g. adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation,com.android.foo.AnotherAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner
- 以上全部參數也能夠經過<meta-data>標籤配置在AndroidManifest文件,好比 <meta-data android:name="listener" android:value="com.foo.Listener"/>,經過shell命令 傳入的參數將覆蓋AndroidManifest文件中配置的參數。
5 開始容易寫錯?debug
AS ——Select Run/Debug —— Configuration ——Edit Configuration ——配置 ——OKcode
運行完成,下方會顯示該命令,而後copy過來。