設定 AndroidX Test 的專案

AndroidX Test 是一組 Jetpack 程式庫,可讓您對 Android 應用程式執行測試。並且提供了一系列工具,協助您編寫這些測試。

舉例來說,AndroidX Test 提供 JUnit4 規則,以啟動活動並在 JUnit4 測試中與這些規則互動。此外還提供 UI 測試架構,例如 Espresso、UI Automator 和 Robolectric 模擬器。

新增 AndroidX 測試庫

如要使用 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 測試依附元件:

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 類別,例如 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" />