针对 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 软件包,将其软件包名称添加到 dependencies 部分。例如,如需添加 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" />