本文主要是介绍AndroidStudio下使用gradle打包测试用例的命令行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取包名、资源文件id
假设已经有一个待测试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目录下编写黑盒测试代码。
TestRunner的编译和执行
哈哈,可以实现自动化或用脚本控制测试了。之前一直用gradle cC,虽然可以对整个module进行编译测试并生成测试报告,但是不理解原理。
前面创建好的项目,如果直接使用Android Studio进行测试运行,实际上Android Studio会执行以下操作:
- gradle assembleDebug,打包主目录main中的待测试APK
- gradle assembleDebugAndroidTest,打包测试目录androidTest中的TestRunner
- adb install -r app/build/outputs/apk/app-debug.apk,安装build目录生成的待测试APK
- adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk,安装build目录生成的TestRunner
- adb shell am instrument -w com.jzj1993.test_instrumentation.test/android.test.InstrumentationTestRunner,运行TestRunner
现在只需要手动执行其中的部分操作即可,即:打包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
运行测试时的一些报错
找不到TestRunner
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。
找不到被测试包 Target Package
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和安装的包不一致。
找不到目标类 ClassNotFound
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抛出的异常。常见有几种可能:
- 测试代码中的目标包名、类名,和实际安装在设备上的APK包名、类名不一致;
- TestRunner的包名和被测试包名不匹配(一般情况下,例如被测试APK包名为com.example.target,则TestRunner包名应为com.example.target.test);
- 其他
签名不一致
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
这篇关于AndroidStudio下使用gradle打包测试用例的命令行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!