针对 AndroidX Test 设置项目

AndroidX Test 是 Jetpack 库的集合,可让您运行测试 。它还提供了一系列工具来帮助您编写 测试。

例如,AndroidX Test 提供 JUnit4 规则来启动 activity 和 在 JUnit4 测试中与它们进行交互。它还包含界面测试框架,例如 例如 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 软件包,请将 以下几行代码:
GroovyKotlin
dependencies {
       
...
        androidTestImplementation
"androidx.test.espresso:espresso-core:$espressoVersion"
   
}
dependencies {
       
...
        androidTestImplementation
('androidx.test.espresso:espresso-core:$espressoVersion')
   
}

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

GroovyKotlin
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"
}
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")
}

版本说明页面包含一个表格,其中每个表格 工件。

如需具体参考,请参阅 Package IndexClass Index 有关这些库的文档

使用已弃用的类的项目

如果您的应用使用的测试依赖于基于 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" />