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 パッケージごとに、そのパッケージ名を依存関係セクションに追加します。たとえば、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" />