Espresso-testing-setup-instructions

提供:Dev Guides
移動先:案内検索

エスプレッソテストフレームワーク-セットアップ手順

この章では、espressoフレームワークをインストールし、espressoテストを記述してAndroidアプリケーションで実行するように設定する方法を理解しましょう。

前提条件

Espressoは、Android SDKを使用してJava/Kotlin言語で開発されたAndroidアプリケーションをテストするためのユーザーインターフェイステストフレームワークです。 したがって、エスプレッソの唯一の要件は、JavaまたはKotlinでAndroid SDKを使用してアプリケーションを開発することであり、最新のAndroid Studioを使用することをお勧めします。

エスプレッソフレームワークで作業を開始する前に適切に設定する項目のリストは次のとおりです-

  • 最新のJava JDKをインストールし、JAVA_HOME環境変数を構成します。
  • 最新のAndroid Studio(バージョン3.2。 以上)。
  • SDKマネージャーを使用して最新のAndroid SDKをインストールし、ANDROID_HOME環境変数を構成します。
  • 最新のGradle Build Toolをインストールし、GRADLE_HOME環境変数を設定します。

EspressoTestingフレームワークを構成する

当初、エスプレッソテストフレームワークは、Androidサポートライブラリの一部として提供されています。 その後、Androidチームは新しいAndroidライブラリAndroidXを提供し、最新のエスプレッソテストフレームワーク開発をライブラリに移行します。 エスプレッソテストフレームワークの最新の開発(Android 9.0、APIレベル28以上)は、AndroidXライブラリで行われます。

プロジェクトにエスプレッソテストフレームワークを含めることは、エスプレッソテストフレームワークをアプリケーションgradleファイルapp/build.gradleの依存関係として設定するのと同じくらい簡単です。 完全な構成は次のとおりです。

Androidサポートライブラリを使用して、

android {
   defaultConfig {
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
}
dependencies {
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espressocore:3.0.2'
}

AndroidXライブラリを使用して、

android {
   defaultConfig {
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }
}
dependencies {
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.androidx.test:runner:1.0.2'
   androidTestImplementation 'com.androidx.espresso:espresso-core:3.0.2'
}

_android/defaultConfig_の_testInstrumentationRunner_は、_AndroidJUnitRunner_クラスを設定して、インストルメンテーションテストを実行します。 _dependencies_の最初の行には_JUnit_テストフレームワークが含まれ、_dependencies_の2行目にはテストケースを実行するテストランナーライブラリが含まれ、最後に_dependencies_の3行目にはエスプレッソテストフレームワークが含まれます。

デフォルトでは、Androidスタジオは、espressoテストフレームワーク(Androidサポートライブラリ)を依存関係として設定し、Androidプロジェクトを作成すると、gradleは必要なライブラリをMavenリポジトリからダウンロードします。 簡単なHello world Androidアプリケーションを作成し、エスプレッソテストフレームワークが適切に構成されているかどうかを確認しましょう。

新しいAndroidアプリケーションを作成する手順は以下のとおりです-

  • Android Studioを起動します。
  • ファイル→新規→新規プロジェクトを選択します。
  • Application Name(HelloWorldApp)とCompany domain(espressosamples.finddevguides.com)を入力し、[_ Next_]をクリックします。

Androidアプリケーション

Androidプロジェクトを作成するには、

  • API 15:Android 4.0.3(IceCreamSandwich)として最小のAPIを選択し、[次へ]をクリックします。

ターゲットAndroidデバイス

Androidデバイスをターゲットにするには、

  • [空のアクティビティ]を選択し、[次へ]をクリックします。

空のアクティビティ

アクティビティをモバイルに追加するには、

  • メインアクティビティの名前を入力し、[完了]をクリックします。

メインアクティビティ

アクティビティを構成するには、

  • 新しいプロジェクトが作成されたら、_app/build.gradle_ファイルを開き、その内容を確認します。 ファイルの内容は次のとおりです。
apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.finddevguides.espressosamples.helloworldapp"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'),    'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espressocore:3.0.2'
}

最後の行は、エスプレッソテストフレームワークの依存関係を指定します。 デフォルトでは、Androidサポートライブラリが設定されています。 メニューで_Refactor_→Migrate to _AndroidX_をクリックして、_AndroidX_ライブラリを使用するようにアプリケーションを再構成できます。

Espresso Testing Framework

Androidxに移行するには、

  • これで、_app/build.gradle_が次のように変更されます。
apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.finddevguides.espressosamples.helloworldapp"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
   implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'androidx.test:runner:1.1.1'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

現在、最後の行にはAndroidXライブラリのエスプレッソテストフレームワークが含まれています。

デバイスの設定

テスト中は、テストに使用されるAndroidデバイスのアニメーションをオフにすることをお勧めします。 これにより、アイドリングリソースを確認する際の混乱が軽減されます。

Androidデバイスでアニメーションを無効にする方法を見てみましょう–(設定→開発者オプション)、

  • ウィンドウアニメーションスケール
  • 遷移アニメーションスケール
  • アニメーターの継続時間スケール

[設定]画面で[開発者オプション]メニューが使用できない場合は、[電話について]オプションで[番号を作成]を数回クリックします。 これにより、_Developer Option_メニューが有効になります。