仪器测试
插桩测试提供了一种通过 am instrument 命令启动的特殊测试执行环境。
主要概念
- 必须在应用软件包中声明插桩,方法是在应用软件包清单的 标记下嵌套 标记。
- 每个 必须包含:
第一: android:name 属性:它应该是测试应用中包含的 Instrumentation的子类的名称,该测试应用通常是正在使用的测试运行程序,例 如android.support.test.runner.AndroidJUnitRunner
第二:必须定义 android:targetPackage 属性。其值应该设置为接受测试的应用软件包。
步骤总结
1.下面是框架服务封闭测试的常见位置:
frameworks/base/core/tests/coretests
frameworks/base/services/tests/servicestests
2.使用 m 命令构建测试模块
m FrameworksCoreTests
或者
make FrameworksCoreTests
3.安装生成的 APK
adb install -r ${OUT}/xxx/FrameworksCoreTests.apk
-w表示等待执行完成后才返回
-e表示带两个参数,将这两个参数作为键值对传递testRunner
1.apk 中的所有测试
adb shell am instrument -w com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
2.特定 Java 软件包下的所有测试
adb shell am instrument -w -e package android.animation \
com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
3.特定类下的所有测试
adb shell am instrument -w -e class \
android.animation.AnimatorSetEventsTest \
com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
4.特定的测试方法
adb shell am instrument -w -e class \
android.animation.AnimatorSetEventsTest#testCancel \
com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
测试结果
adb shell am instrument -w -e class android.animation.AnimatorSetEventsTest#testCancel com.android.frameworks.coretests /android.support.test.runner.AndroidJUnitRunner
android.animation.AnimatorSetEventsTest:.
Time: 5.446
OK (1 test)
参考链接:https://source.android.google.cn/docs/core/tests/development/instrumentation?hl=zh-cn