针对 AndroidX Test 设置项目

AndroidX Test 是 Jetpack 库的集合,可让您针对 Android 应用运行测试。它还提供一系列工具来帮助您编写这些测试。

例如,AndroidX Test 提供了 JUnit4 规则,以在 JUnit4 测试中启动 activity 并与之交互。它还包含界面测试框架,例如 Espresso、UI Automator 和 Robolectric 模拟器。

添加 AndroidX Test 库

为了使用 AndroidX Test,您必须在开发环境中修改应用项目的依赖项。

添加 Gradle 依赖项

如需修改应用项目的依赖项,请完成以下步骤:

  • 第 1 步:打开 Gradle 模块的 build.gradle 文件。
  • 第 2 步:在“repositories”部分中,确保显示了 Google 的 Maven 代码库:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • 第 3 步:对于要使用的每个 AndroidX Test 软件包,将其软件包名称添加到依赖项部分。例如,如需添加 espresso-core 软件包,请添加以下代码行:

Groovy

dependencies {
        ...
        androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    }

Kotlin

dependencies {
        ...
        androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')
    }

以下是最常用的可用 AndroidX Test 依赖项:

Groovy

dependencies {
    // Core library
    androidTestImplementation "androidx.test:core:$androidXTestVersion0"

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation "androidx.test:runner:$testRunnerVersion"
    androidTestImplementation "androidx.test:rules:$testRulesVersion"

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
    androidTestImplementation "androidx.test.ext:truth:$truthVersion"

    // Espresso dependencies
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK’"s compile classpath or the test APK
    // classpath.
    androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
}

Kotlin

dependencies {
    // Core library
    androidTestImplementation("androidx.test:core:$androidXTestVersion")

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation("androidx.test:runner:$testRunnerVersion")
    androidTestImplementation("androidx.test:rules:$testRulesVersion")

    // Assertions
    androidTestImplementation("androidx.test.ext:junit:$testJunitVersion")
    androidTestImplementation("androidx.test.ext:truth:$truthVersion")

    // Espresso dependencies
    androidTestImplementation( "androidx.test.espresso:espresso-core:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-contrib:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-intents:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-accessibility:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-web:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso.idling:idling-concurrent:$espressoVersion")

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK"s compile classpath or the test APK
    // classpath.
    androidTestImplementation( "androidx.test.espresso:espresso-idling-resource:$espressoVersion")
}

版本说明页面包含一个表格,其中列出了每个工件的最新版本。

如需查看这些库的具体参考文档,请参阅软件包索引类索引

使用已废弃类的项目

如果您的应用使用的测试依赖于已废弃的基于 JUnit3 的 android.test 类(例如 InstrumentationTestCaseTestSuiteLoader),请在文件的 android 部分添加以下行:

android {
    ...
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
  }

添加清单声明

如需运行依赖于已废弃的基于 JUnit3 的 android.test 类的测试,请在测试应用清单中添加必要的 <uses-library> 元素。例如,如果您添加依赖于 android.test.runner 库的测试,请将以下元素添加到应用的清单中:

<!-- You don't need to include android:required="false" if your app's

   minSdkVersion is 28 or higher. -->

<uses-library android:name="android.test.runner"

       android:required="false" />

如需确定包含基于 JUnit 的给定类的库,请参阅基于 JUnit 的库

使用已废弃的类以及以 Android 9 或

本节中的指南仅适用于以下情况:以 Android 9(API 级别 28)或更高版本为目标平台,应用的最低 SDK 版本设为 Android 9。

android.test.runner 库隐式依赖于 android.test.baseandroid.test.mock 库。如果您的应用仅使用 android.test.baseandroid.test.mock 中的类,您可以单独添加这些库:

<!-- For both of these declarations, you don't need to include
   android:required="false" if your app's minSdkVersion is 28
   or higher. -->

<uses-library android:name="android.test.base"
       android:required="false" />
<uses-library android:name="android.test.mock"
       android:required="false" />