官网关于该命令的详解: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 -p file
:将分析数据写入 file
。-w
:测试运行器需要使用此选项。-w --no-window-animation
:运行时关闭窗口动画。--user user_id | current
:指定仪器在哪个用户中运行;如果未指定,则在当前用户中运行。3 怎么运行手机中现有的instrumentation, 并输出详细结果,同时将profiling性能数据写到本地文件中?
4 命令的具体使用,比如com.le.tcauto.uitest.test是包含所有测试代码的应用的包名:(来自:http://blog.csdn.net/swordgirl2011/article/details/50881390)
adb shell am instrument -w -r -e class com.letv.leview.setproxy com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
5 开始容易写错?
AS ——Select Run/Debug —— Configuration ——Edit Configuration ——配置 ——OK
运行完成,下方会显示该命令,然后copy过来。
假设已经有一个待测试APK,将其安装到安卓设备上。通过adb指令、Android Studio的Device Monitor可以获取其页面的包名、控件的id等信息。
用adb查看当前的Activity
adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p'
Running activities (most recent first):
TaskRecord{5323d558 #32 A com.jzj1993.unittest U 0}
Run #1: ActivityRecord{5311cd60 u0 com.jzj1993.unittest/.MainActivity}
TaskRecord{5309f8c8 #2 A com.android.launcher U 0}
Run #0: ActivityRecord{52fc9ac0 u0 com.android.launcher/com.android.launcher2.Launcher}
如果有待测试APK的签名源文件,则直接配置给测试工程即可。如果没有,可以通过工具对待测试APK进行重新签名,使其与TestRunner的签名一致即可。
用Android Studio创建一个Android项目,包名和被测试APK一致,签名与待测APK保持一致。
按照白盒测试一样的方式进行配置。可参考本系列文章的第三篇。
代码主目录main中不用写任何代码(写了也不影响)。
在androidTest目录下编写黑盒测试代码。
前面创建好的项目,如果直接使用Android Studio进行测试运行,实际上Android Studio会执行以下操作:
现在只需要手动执行其中的部分操作即可,即:打包TestRunner、安装TestRunner、运行TestRunner,最终命令行输出测试成功的信息,同时可以看到安卓设备中进行黑盒测试的APK已经按照测试代码执行。
gradle clean
gradle assembleDebugAndroidTest
adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk
adb shell am instrument -w com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner
com.jzj1993.test_instrumentation.ApplicationTest:..
com.jzj1993.test_instrumentation.ToastTest:.
Test results for InstrumentationTestRunner=...
Time: 5.311
OK (3 tests)
在adb shell am instrument命令中,还可以指定-e参数,只运行TestRunner中指定的类。例如我可以在测试代码中写一个ToastTest和一个DialogTest,编译后如果不指定-e参数,则默认每个Test都会先后被执行,而如果指定-e参数,可以只运行ToastTest。
adb shell am instrument -e class com.jzj1993.test_instrumentation.ToastTest -w com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner
adb shell am instrument -w com.jzj1993.black.test/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.jzj1993.black.test/android.test.InstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.jzj1993.black.test/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:802)
at com.android.commands.am.Am.onRun(Am.java:242)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:75)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)
一般是因为TestRunner没有安装成功,或者在命令中写错包名等,可以用adb shell pm list instrumentation
查看已经安装的TestRunner。
adb shell am instrument -w com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.jzj1993.test_instrumentation
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:802)
at com.android.commands.am.Am.onRun(Am.java:242)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:75)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)
一般是因为待测试包没有安装成功,或者TestRunner的Target和安装的包不一致。
adb shell am instrument -w com.jzj1993.black.test.test/android.test.InstrumentationTestRunner
android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:
Error in testSuiteConstructionFailed:
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.jzj1993.black.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at com.jzj1993.black.test.BlackBoxTest.<init>(BlackBoxTest.java:17)
... 18 more
Caused by: java.lang.NoClassDefFoundError: com/jzj1993/black/MainActivity
... 22 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.jzj1993.black.MainActivity" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.jzj1993.black.test.test-1.apk", zip file "/data/app/com.jzj1993.black.test-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jzj1993.black.test.test-1, /data/app-lib/com.jzj1993.black.test-2, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
... 22 more
Test results for InstrumentationTestRunner=.E
Time: 0.008
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
这个错误是TestRunner抛出的异常。常见有几种可能:
adb shell am instrument -w com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Permission Denial: starting instrumentation ComponentInfo{com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner} from pid=9230, uid=9230 not allowed because package com.jzj1993.test_instrumentation.test does not have a signature matching the target com.jzj1993.test_instrumentation
INSTRUMENTATION_STATUS_CODE: -1
java.lang.SecurityException: Permission Denial: starting instrumentation ComponentInfo{com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner} from pid=9230, uid=9230 not allowed because package com.jzj1993.test_instrumentation.test does not have a signature matching the target com.jzj1993.test_instrumentation
at android.os.Parcel.readException(Parcel.java:1431)
at android.os.Parcel.readException(Parcel.java:1385)
at android.app.ActivityManagerProxy.startInstrumentation(ActivityManagerNative.java:2938)
at com.android.commands.am.Am.runInstrument(Am.java:801)
at com.android.commands.am.Am.onRun(Am.java:242)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:75)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)原网址
原网址:http://www.paincker.com/android-test-7
https://www.cnblogs.com/insist8089/p/6897037.html