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

AndroidX Test は、テストを実行できる Jetpack ライブラリのコレクションである ダウンロードされます。また、こうしたデータの作成に役立つ一連のツールも テストです。

たとえば、AndroidX Test には、アクティビティを開始し、 JUnit4 テストでそれらと相互作用します。また、UI テスト フレームワークも用意されています。 Espresso、UI Automator、Robolectric シミュレータなどがあります。

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" />