AndroidX Test 是 Jetpack 库的集合,可用于针对 Android 应用运行测试。它还提供一系列工具来帮助您编写这些测试。
例如,AndroidX Test 提供 JUnit4 规则,用于在 JUnit4 测试中启动 activity 并与之交互。它还包含 Espresso、UI Automator 和 Robolectric 模拟器等 UI 测试框架。
添加 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") }
《版本说明》页面包含一个表格,其中列出了每个 artifact 的最新版本。
如需这些库的特定参考文档,请参阅《软件包索引》或《类索引》。
使用废弃类的项目
如果您的应用使用依赖于废弃的基于 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" />