

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 适用于 Android 的 Instrumentation 与 AWS Device Farm
<a name="test-types-android-instrumentation"></a>

Device Farm 支持安卓版仪器（JUnit、Espresso、Robotium 或任何基于仪器的测试）。

Device Farm 还提供了示例 Android 应用程序，以及指向三个 Android 自动化框架（包括 Instrumentation (Espresso)）中的工作测试的链接。[安卓版 Device Farm 示例应用程序](https://github.com/awslabs/aws-device-farm-sample-app-for-android)可在上下载 GitHub。

有关 Device Farm 中测试的更多信息，请参阅 [AWS Device Farm 中的测试框架和内置测试](test-types.md)。

**Topics**
+ [什么是 Instrumentation？](#test-types-android-instrumentation-what-is)
+ [Android Instrumentation 测试的注意事项](#test-types-android-instrumentation-settings)
+ [标准模式测试解析](#test-types-android-standard-mode-test-parse)
+ [将 Android Instrumentation 与 Device Farm 集成](test-types-android-instrumentation-integrate.md)

## 什么是 Instrumentation？
<a name="test-types-android-instrumentation-what-is"></a>

使用 Android Instrumentation，您可在测试代码中调用回调方法，从而能够逐步运行组件的生命周期，就像在调试组件一样。有关更多信息，请参阅《Android Developer Tool》[https://developer.android.com/studio/test/test-in-android-studio#test_types_and_locations](https://developer.android.com/studio/test/test-in-android-studio#test_types_and_locations) 文档的“Test types and locations”** 一节中的 *Instrumented tests*。

## Android Instrumentation 测试的注意事项
<a name="test-types-android-instrumentation-settings"></a>

使用 Android Instrumentation 时应注意以下建议和注意事项。

**查看 Android 操作系统的兼容性**  
 请查看 [Android 文档](https://developer.android.com/jetpack/androidx/releases/test#orchestrator-1.5.0)，确保 Instrumentation 与您的 Android 操作系统版本兼容。

**从命令行运行**  
 要通过命令行运行 Instrumentation 测试，请按照 [Android 文档](https://developer.android.com/training/testing/instrumented-tests/androidx-test-libraries/runner#enable-command)进行操作。

**系统动画**  
 根据[有关 Espresso 测试的 Android 文档](https://developer.android.com/training/testing/espresso)，建议在真实设备上测试时关闭系统动画。Device Farm 使用 an [droid.support.test.runner. JUnit](http://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html) **Android Runner 插件测试运行器执行时，它会自动禁用窗口****动画比例、**过渡**动画比例和动画师持续时间缩放**设置。

**测试记录器**  
Device Farm 支持具有 record-and-playback脚本工具的框架，例如 Robotium。

## 标准模式测试解析
<a name="test-types-android-standard-mode-test-parse"></a>

在标准运行模式下，Device Farm 会解析您的测试套件并识别它将运行的唯一测试类和方法。此启用方式是通过名为 [Dex Test Parser](https://github.com/linkedin/dex-test-parser) 的工具实现的。

当给定一个 Android instrumenting .apk 文件作为输入时，解析器会返回符合 JUnit 3 和 JUnit 4 惯例的测试的完全限定方法名称。

要在本地环境中对此进行测试，请执行以下操作：

1. 下载 [https://github.com/linkedin/dex-test-parser](https://github.com/linkedin/dex-test-parser) 二进制文件。

1. 运行以下命令以获取将在 Device Farm 上运行的测试方法列表：

   ```
   java -jar parser.jar path/to/apk path/for/output
   ```