Android 单元测试-运行

运行测试有多种方式:

  1. 通过IDE运行;
  2. 通过gradle任务运行;
  3. 手动通过adb命令运行;

其中 1 和 2 实际上最终都是通过adb命令运行的,下面分别进行说明。

AndroidStudio 运行

使用IDE运行时,可以单个测试函数运行,也可以单个类运行,也可以按包运行或者按模块运行。不过本地单元测试和AndroidTest的运行略有区别。

javaTest
  • 按方法: 直接选中方法,右键运行即可
  • 按类: 直接选中类,右键运行即可
  • 按包: 选中包,右键-"Run test in ..."
  • 按模块: 选中模块,右键-"Run All test"
androidTest
  • 按方法:

    • 选中 @Test 标记的方法,右键或者点击方法左边的绿色三角,然后 Modify Run Configuration,然后直接保存
    • 再次按上述操作,选择上下文菜单中的Run '方法名称()' 运行测试
  • 按测试类:

    • 选中测试类,右键或者点击类左边的两个绿色三角图标,然后 Modify Run Configuration,然后直接保存
    • 再次按上述操作,选择上下文菜单中的Run '类名称' 运行测试
  • 按包:

    • Project 工具窗口中选中包 - 右键 - Run Test In xxx.xx.xx
  • 按模块

    • 选中模块 - 右键 - Run All Test

Gradle 任务运行

Gradle 运行时,只能运行所有测试。

javaTest

通过运行 testDebugUnitTest 任务即可启动测试

运行完成后的报告位于 build/reports/tests 目录。

androidTest

通过运行 connectedDebugAndroidTest 任务即可启动测试

运行完成后的报告位于 build/reports/androidTests 目录。

Adb命令运行

通过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.

总结

  1. 日常编写时,直接使用AndroidStudio运行测试即可;
  2. 需要生成报告或者覆盖率报告时则可使用gradle任务运行测试(可以生成报告);
  3. 如果是CI、CD中使用,则可以使用gradle任务运行测试,无聊的或者有更加定制化的需求的可以使用adb命令运行测试。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值