赞
踩
运行测试有多种方式:
其中 1 和 2 实际上最终都是通过adb命令运行的,下面分别进行说明。
使用IDE运行时,可以单个测试函数运行,也可以单个类运行,也可以按包运行或者按模块运行。不过本地单元测试和AndroidTest的运行略有区别。
右键-"Run test in ..."
右键-"Run All test"
按方法:
@Test
标记的方法,右键或者点击方法左边的绿色三角,然后 Modify Run Configuration
,然后直接保存Run '方法名称()'
运行测试按测试类:
Modify Run Configuration
,然后直接保存Run '类名称'
运行测试按包:
右键 - Run Test In xxx.xx.xx
按模块
右键 - Run All Test
Gradle 运行时,只能运行所有测试。
通过运行 testDebugUnitTest
任务即可启动测试
运行完成后的报告位于 build/reports/tests
目录。
通过运行 connectedDebugAndroidTest
任务即可启动测试
运行完成后的报告位于 build/reports/androidTests
目录。
通过adb命令可以运行 androidTest,实际上前面Gradle任务及AndroidStudio运行androidTest也是通过adb命令完成的。
故我们也可以手动运行 adb shell am instrument
命令来运行 android 单元测试,一般的用法如下,手动运行时完成的结果会在命令终端中输出。
按方法:
# 通过 #useAppContext 指定要测试的方法
adb shell am instrument -w -m -e debug false -e class 'com.github.hanlyjiang.app.ExampleInstrumentedTest#useAppContext' com.github.hanlyjiang.app.test/androidx.test.runner.AndroidJUnitRunner
按类:
adb shell am instrument -w -m -e debug false -e class 'com.github.hanlyjiang.app.ExampleInstrumentedTest' com.github.hanlyjiang.app.test/androidx.test.runner.AndroidJUnitRunner
按包:
adb shell am instrument -w -r -e debug false -e package 'com.github.hanlyjiang.app.' com.github.hanlyjiang.app.test/androidx.test.runner.AndroidJUnitRunner
instrument 详细用法参考:
instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w] [--user <USER_ID> | current] [--no-hidden-api-checks [--no-test-api-access]] [--no-isolated-storage] [--no-window-animation] [--abi <ABI>] <COMPONENT> Start an Instrumentation. Typically this target <COMPONENT> is in the form <TEST_PACKAGE>/<RUNNER_CLASS> or only <TEST_PACKAGE> if there is only one instrumentation. Options are: -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT). Use with [-e perf true] to generate raw output for performance measurements. -e <NAME> <VALUE>: set argument <NAME> to <VALUE>. For test runners a common form is [-e <testrunner_flag> <value>[,<value>...]]. -p <FILE>: write profiling data to <FILE> -m: Write output as protobuf to stdout (machine readable) -f <Optional PATH/TO/FILE>: Write output as protobuf to a file (machine readable). If path is not specified, default directory and file name will be used: /sdcard/instrument-logs/log-yyyyMMdd-hhmmss-SSS.instrumentation_data_proto -w: wait for instrumentation to finish before returning. Required for test runners. --user <USER_ID> | current: Specify user instrumentation runs in; current user if not specified. --no-hidden-api-checks: disable restrictions on use of hidden API. --no-test-api-access: do not allow access to test APIs, if hidden API checks are enabled. --no-isolated-storage: don't use isolated storage sandbox and mount full external storage --no-window-animation: turn off window animations while running. --abi <ABI>: Launch the instrumented process with the selected ABI. This assumes that the process supports the selected ABI.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。