官網關於該命令的詳解:https://developer.android.com/studio/command-line/adb.html?hl=zh-cn
1 Instrument是什么?
instrument為am命令的一個子命令。用於啟動一個Instrumentation測試。首先連接手機或者模擬器,通過adb 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性能數據寫到本地文件中?
- 先列出手機中已安裝的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)
- 運行所有的用例: 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 開始容易寫錯?
AS ——Select Run/Debug —— Configuration ——Edit Configuration ——配置 ——OK
運行完成,下方會顯示該命令,然后copy過來。
6 工程實踐,見AS