SuperAPI AI 工具集

running-instrumented-tests-via-adb

测试与质量

查看项目

更新于 10-16 01:49

技能介绍

用途:直接通过 adb shell am instrument -w -r 运行 Android 仪器化测试,不经过 Gradle。适合已自行管理 APK 的 CI 脚本、多设备分片执行,以及针对单个测试方法的快速调试。

主要能力:支持按测试类或单个方法(类#方法)、@SmallTest/@MediumTest/@LargeTest、annotation/notAnnotation 筛选;用 numShards/shardIndex 在多设备分片;用 debug true 等待调试器附加;用 listener 注册 RunListener;用 coverage true 收集 JaCoCo 覆盖率。也涵盖 AndroidX Test Orchestrator 的 clearPackageData 与 targetInstrumentation 配置。

必要用法:命令为 adb shell am instrument -w -r [-e key value ...]。-w 让 shell 等待测试结束,-r 输出原始可解析结果,CI 中必须同时使用。单方法选择用 # 分隔类与方法。CI 必须解析 stdout 中的 INSTRUMENTATION_STATUS_CODE: -1(错误)或 -2(失败)判断成败,因为 am instrument 无论测试结果都退出 0。Orchestrator 场景下,位置参数应为 androidx.test.orchestrator/.AndroidTestOrchestrator,被包装的 AndroidJUnitRunner 通过 -e targetInstrumentation 传入。

限制:不能依赖 $? 判断测试通过与否;clearPackageData 仅在 Orchestrator 下有效;分片需在不同设备上运行,同一设备会串行执行;单方法选择不能用冒号或点号。运行前建议关闭动画并清除应用数据。未提供的信息暂未提及。