AndroidX Test 用にプロジェクトをセットアップする

AndroidX Test は、Android アプリに対してテストを実行できる Jetpack ライブラリのコレクションです。また、これらのテストの作成に役立つ一連のツールも提供します。

たとえば、AndroidX Test は、JUnit4 テストでアクティビティを開始して操作するための JUnit4 ルールを提供します。また、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")
}

リリースノートのページには、 アーティファクトごとの最新バージョンを示す表が記載されています。

これらのライブラリの具体的なリファレンス ドキュメントについては、パッケージ インデックスまたはクラス インデックスをご覧ください。

非推奨のクラスを使用するプロジェクト

アプリで非推奨の 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.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" />