首页
/ Bazel项目中的Android Instrumentation测试指南

Bazel项目中的Android Instrumentation测试指南

2025-07-05 07:19:17作者:冯爽妲Honey

概述

在Android开发中,Instrumentation测试是一种重要的测试方法,它允许开发者在真实或模拟的Android设备上运行测试代码。Bazel作为一个高效的构建工具,提供了android_instrumentation_test规则来支持这种测试方式。

核心概念

android_instrumentation_test规则

android_instrumentation_test是Bazel提供的一个构建规则,它能够:

  1. 构建测试APK和被测试APK
  2. 在沙盒环境中创建和启动Android模拟器
  3. 安装APK并运行测试
  4. 报告测试结果

工作原理

当首次运行bazel test时,Bazel会执行以下步骤:

  1. 构建测试APK、被测试APK及其依赖项
  2. 创建、启动并缓存干净的模拟器状态
  3. 启动模拟器
  4. 安装APK
  5. 使用Android Test Orchestrator运行测试
  6. 关闭模拟器
  7. 报告结果

后续测试运行时,Bazel会从缓存的干净状态启动模拟器,确保测试环境的一致性。

环境准备

系统要求

  • 操作系统:Linux(推荐Ubuntu 16.04或18.04)
  • Bazel版本:0.12.0或更高
  • 硬件加速:需要KVM支持
  • 图形界面:Xvfb(用于无头测试)
  • 32位库:在64位系统上需要安装

依赖安装

# 安装KVM相关组件
apt-get install cpu-checker && kvm-ok

# 安装Xvfb
apt-get install xvfb

# 安装32位库
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386

项目配置

BUILD文件示例

android_instrumentation_test(
    name = "my_test",
    test_app = ":my_test_app",
    target_device = "@android_test_support//tools/android/emulated_devices/generic_phone:android_23_x86",
)

android_binary(
    name = "my_test_app",
    instruments = ":my_app",
    manifest = "AndroidTestManifest.xml",
    deps = [":my_test_lib"],
)

# 其他相关规则...

WORKSPACE配置

android_sdk_repository(
    name = "androidsdk",
    path = "/path/to/sdk",
)

http_archive(
    name = "android_test_support",
    strip_prefix = "android-test-$COMMIT_HASH",
    urls = ["https://github.com/android/android-test/archive/$COMMIT_HASH.tar.gz"],
)
load("@android_test_support//:repo.bzl", "android_test_repositories")
android_test_repositories()

测试执行

运行配置

在项目根目录的.bazelrc文件中添加以下配置:

# 无头测试配置
test:headless --test_arg=--enable_display=false

# 图形界面测试配置
test:gui --test_env=DISPLAY
test:gui --test_arg=--enable_display=true

# 本地设备测试配置
test:local_device --test_strategy=exclusive
test:local_device --test_arg=--device_broker_type=LOCAL_ADB_SERVER

执行命令

  • 无头测试:bazel test //my/test:target --config=headless
  • 图形界面测试:bazel test //my/test:target --config=gui
  • 本地设备测试:bazel test //my/test:target --config=local_device

高级技巧

Espresso测试配置

对于UI测试,可以使用Espresso框架。配置示例:

maven_install(
    artifacts = [
        "androidx.test.espresso:espresso-core:3.1.1",
        "androidx.test:rules:aar:1.1.1",
        "androidx.test:runner:aar:1.1.1",
        # 其他依赖...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

多API级别测试

可以使用列表推导式创建针对不同API级别的测试目标:

API_LEVELS = ["19", "20", "21", "22"]

[android_instrumentation_test(
    name = "my_test_%s" % level,
    test_app = ":my_test_app",
    target_device = "@android_test_support//tools/android/emulated_devices/generic_phone:android_%s_x86_qemu2" % level,
) for level in API_LEVELS]

日志分析

测试日志

  • 使用--test_output=errors查看失败测试的日志
  • 使用--test_output=all查看所有测试输出
  • 测试日志位于bazel-testlogs/path/to/InstrumentationTestTargetName

模拟器日志

模拟器日志存储在/tmp/emulator_*.log文件中,可以使用以下命令查找最新日志:

ls -1t /tmp/emulator_*.log | head -n 1

总结

Bazel的android_instrumentation_test规则为Android测试提供了强大的支持,通过合理的配置可以实现高效、可靠的自动化测试。本文介绍了从环境准备到测试执行的完整流程,以及一些高级使用技巧,希望能帮助开发者更好地利用Bazel进行Android测试工作。