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过来。