Android 单元测试-运行
运行测试有多种方式:
- 通过IDE运行;
- 通过gradle任务运行;
- 手动通过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
- Project 工具窗口中选中包 -
按模块
- 选中模块 -
右键 - 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 theform <TEST_PACKAGE>/<RUNNER_CLASS> or only <TEST_PACKAGE> if thereis 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 acommon 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 (machinereadable). If path is not specified, default directory and file name willbe used: /sdcard/instrument-logs/log-yyyyMMdd-hhmmss-SSS.instrumentation_data_proto-w: wait for instrumentation to finish before returning. Required fortest 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 hiddenAPI checks are enabled.--no-isolated-storage: don't use isolated storage sandbox andmount 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.
总结
- 日常编写时,直接使用AndroidStudio运行测试即可;
- 需要生成报告或者覆盖率报告时则可使用gradle任务运行测试(可以生成报告);
- 如果是CI、CD中使用,则可以使用gradle任务运行测试,无聊的或者有更加定制化的需求的可以使用adb命令运行测试。
Android 单元测试-运行相关推荐
- android单元测试android环境,基于Robolectric的Android单元测试 —环境搭建与部署运行...
移动端的测试中,因为回归一些逻辑分支比较多的功能时工作量比较大,且不太适合用UI完成,尝试通过单元测试来完成.几经波折终于完成了一个功能的UT用例并在CI上部署运行,现总结如下: 一.Robolect ...
- Android单元测试全解
自动化测试麻烦吗?说实在,麻烦!有一定的学习成本.但是,自动化测试有以下优点: 节省时间:可以指定测试某一个activity,不需要一个个自己点 单元测试:既然Java可以进行单元测试,Andro ...
- Android 单元测试学习计划
网上查了一下Android单元测试相关的知识点,总结了一个学习步骤: 1. 什么是单元测试 2. 单元测试正反面: 2.1. 重要性 2.2. 缺陷 2.3. 策略 3. 单元测试的基础知识: 3.1 ...
- Android单元测试 - 几个重要问题
前言 已经一个月没写文章了,由于9月份在plan国庆旅行计划,国庆前前后后去了14天旅行,所以没时间写,哈哈. 言归正传,上一篇文章<Android单元测试 - 如何开始?>介绍了几款单元 ...
- Android单元测试研究与实践
处于高速迭代开发中的Android项目往往需要除黑盒测试外更加可靠的质量保障,这正是单元测试的用武之地.单元测试周期性对项目进行函数级别的测试,在良好的覆盖率下,能够持续维护代码逻辑,从而支持项目从容 ...
- Android单元测试 - Sqlite、SharedPreference、Assets、文件操作 怎么测?
前言 上篇<Android单元测试 - 几个重要问题> 讲解了"何解决Android依赖.隔离Native方法.静态方法.RxJava异步转同步"这几个Presente ...
- Android单元测试 Instrumentation
开发中我们需要对部分功能进行单元测试,启动Activity来测试部分小功能,有点小题大作,杀鸡用牛刀. 我们可以用Android单元测试 Instrumentation 本篇只是入门,起到抛砖的效果 ...
- (转)Android单元测试
关键字: camera unit test android源代码中每个app下中都自带了一个test用例,下面主要介绍下camra单元测试用例 在AndroidManifest.xml中标明了测试用例 ...
- Android单元测试思路
如果想在android里面做单元测试,有两条基本的路子可行. 第一, 就是java程序员最为熟悉和常用的JUnit, 但是由于目前android sdk (version 1.1)中只是提供了stub ...
最新文章
- pg 时间戳 能与整数比较大小吗_小学数学55组重要知识“顺口溜”+必考题型口诀,一遍就能记住!...
- linux kernel 开发详细文档 安装方法
- 巴士云_“相见不如在线”巴士五公司“云面试”汽修工和驾驶员
- mxnet基础到提高(5)-- 卷积神经网络基础(1)
- C/C++中调用api设置mysql连接的编码方式
- 最优化课程(part1)
- SIT与UAT的分别
- TensorFlow 2.0 - tf.data.Dataset 数据预处理 猫狗分类
- 修理计算机英语,英语口语对话:修理电脑
- StackRec: 通过迭代堆叠实现推荐系统的高效训练
- Struts2工作流程
- hdu5651 xiaoxin juju needs help(逆元)
- MAC查看CPU信息
- 免费的短信验证码接口
- windows 7远程桌面和被远程连接电脑启动远程桌面服务
- 计算机卡和c盘东西多有关吗,电脑c盘东西装太多会卡吗
- Ubuntu18.04 使用gnome-tweak美化系统主题
- MAC无法挂载移动硬盘
- 评估离岸或者近岸敏捷供应商
- list对象转map stream /去重( 根据属性转Map或者分组)
热门文章
- Latex作者较多时的处理方法
- Mate 40系列发布 搭载华为运动健康服务带来健康数字生活
- 【HMS Core】【FAQ】【Health Kit】运动健康服务常见错误码合集 403、401、1001、20023
- 晨起管家----蓝牙传图V2.5
- Nacos配置读取失败 parse data from Nacos error,dataId
- python日课:NumPy ndarray
- 栈内存与堆内存,堆内存的使用与理解。
- OpenCV边缘检测(二)——Sobel边缘检测
- 阿里巴巴达摩院发布2022十大科技趋势,今年的预测有哪些亮点?
- android系统坏掉了怎么办,手机主板坏了怎么办?如何解决?