AndroidX Test 是一系列 Jetpack 程式庫,可讓您針對 Android 應用程式執行測試。還提供一系列的工具,協助您編寫這些測試。
舉例來說,AndroidX Test 提供 JUnit4 規則,用來啟動活動並在 JUnit4 測試中與其互動。其中也包含 UI 測試架構,例如 Espresso、UI Automator 和 Robolectric 模擬工具。
新增 AndroidX Test 程式庫
如要使用 AndroidX Test,您必須在開發環境中修改應用程式專案的依附元件。
新增 Gradle 依附元件
如要修改應用程式專案的依附元件,請完成下列步驟:
- 步驟 1:開啟 Gradle 模組的
build.gradle
檔案。 - 步驟 2:在存放區部分中,確認 Google 的 Maven 存放區會顯示:
allprojects {
repositories {
jcenter()
google()
}
}
- 步驟 3:針對您要使用的每個 AndroidX 測試套件,將套件名稱新增至依附元件區段。舉例來說,如要新增
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" />