为了使用 AndroidX Test,您需要添加几个测试专用库。本指南介绍如何在应用的项目文件中声明这些库,并说明如何组织 AndroidX Test 库。
添加 AndroidX Test 库
为了使用 AndroidX Test,您必须在开发环境中修改应用项目的类路径依赖项和清单,如下面几部分中所示。
添加 Gradle 依赖项
如需修改应用项目的类路径依赖项,请完成以下步骤:
- 打开应用的
build.gradle
文件。 -
在
repositories
部分中,确保显示了 Google's Maven 代码库:allprojects { repositories { jcenter() google() } }
-
对于您希望使用的每个 AndroidX Test 软件包,将其软件包名称添加到
dependencies
部分。例如,如需添加espresso-core
软件包,请添加以下行:dependencies { ... androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' }
注意:使用动态依赖项(例如
espresso-core:3.1.0+
)可能会导致意外的版本更新和回归不兼容问题。我们建议您明确指定一个特定的版本号(例如espresso-core:3.1.0
)。 - 如果您的应用构建的测试依赖于基于 JUnit 的类,例如
Assert
和TestSuiteLoader
,请在文件的android
部分添加以下行:android { ... // Gradle automatically adds 'android.test.runner' as a dependency. useLibrary 'android.test.runner' useLibrary 'android.test.base' useLibrary 'android.test.mock' }
注意:您只需要添加包含应用中所用类的库。如需查看出现在每个库中的类的列表,请参阅基于 JUnit 的库。
添加清单声明
如需运行依赖于基于 JUnit 的类的测试,请将必要的 <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(API 级别 28)或更高版本时,本部分中的指导才适用。
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" />
AndroidX Test 依赖项列表
AndroidX Test 包含许多依赖项,如下面几部分中所示。
Gradle 依赖项
AndroidX Test 中提供了以下基于 Gradle 的依赖项:
dependencies {
// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
// 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:3.1.0'
}
如通过 Maven 代码库添加 Gradle 依赖项中所述,您可以通过在模块级 build.gradle
文件中添加每个依赖项,将这些依赖项添加到您的开发环境。
基于 JUnit 的库
本部分中的列表展示了平台如何在 Android 9(API 级别 28)及更高版本中组织基于 JUnit 的类。
android.test.base
此库包含以下类,每个类都在 junit.framework
软件包中:
Assert
AssertionFailedError
ComparisonFailure
Protectable
Test
TestCase
TestFailure
TestListener
TestResult
TestSuite
此库还包含自 Android 9(API 级别 28)起弃用的以下类:
android.test
软件包
AndroidTestCase
FlakyTest
InstrumentationTestCase
InstrumentationTestSuite
PerformanceTestCase
UiThreadTest
android.test.suitebuilder.annotation
软件包
android.test.runner
此库包含以下类:
android.test
软件包
junit.runner
软件包
此库还包含自 Android 9(API 级别 28)起弃用的以下类:
android.test
软件包
ActivityInstrumentationTestCase
ActivityInstrumentationTestCase2
ActivityTestCase
ActivityUnitTestCase
AndroidTestRunner
ApplicationTestCase
AssertionFailedError
ComparisonFailure
InstrumentationTestRunner
IsolatedContext
MoreAsserts
ProviderTestCase
RenamingDelegatingContext
ServiceTestCase
SingleLaunchActivityTestCase
SyncBaseInstrumentation
TestSuiteProvider
TouchUtils
ViewAsserts
android.test.mock
此库包含以下类,每个类都在 android.test.mock
软件包中:
此库还包含自 Android 9(API 级别 28)起弃用的以下类,每个类都在 android.test.mock
软件包中: