AndroidX Test 是 Jetpack 库的集合,可让您针对 Android 应用运行测试。它还提供了一系列工具来帮助您编写这些测试。
例如,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
软件包,请添加以下代码行:
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
类(例如 InstrumentationTestCase
和 TestSuiteLoader
),请在文件的 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.base
和 android.test.mock
库。如果您的应用仅使用 android.test.base
或 android.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" />