Espresso 測試框架 - 設定指南



在本章中,讓我們瞭解如何安裝 Espresso 框架,配置它以編寫 Espresso 測試並在我們的 Android 應用程式中執行它。

前提條件

Espresso 是一個用於測試使用 Android SDK 以 Java/Kotlin 語言開發的 Android 應用程式的使用者介面測試框架。因此,Espresso 的唯一要求是使用 Java 或 Kotlin 中的 Android SDK 開發應用程式,建議使用最新的 Android Studio。

在我們開始使用 Espresso 框架之前,需要正確配置以下專案:

  • 安裝最新的 Java JDK 並配置 JAVA_HOME 環境變數。

  • 安裝最新的 Android Studio(3.2 或更高版本)。

  • 使用 SDK Manager 安裝最新的 Android SDK 並配置 ANDROID_HOME 環境變數。

  • 安裝最新的 Gradle 構建工具並配置 GRADLE_HOME 環境變數。

配置 Espresso 測試框架

最初,Espresso 測試框架作為 Android Support 庫的一部分提供。後來,Android 團隊提供了一個新的 Android 庫 AndroidX,並將最新的 Espresso 測試框架開發遷移到該庫中。最新的 Espresso 測試框架開發(Android 9.0,API 級別 28 或更高版本)將在 AndroidX 庫中進行。

在專案中包含 Espresso 測試框架就像在應用程式 gradle 檔案 app/build.gradle 中將 Espresso 測試框架設定為依賴項一樣簡單,完整的配置如下:

使用 Android Support 庫:

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 類來執行 Instrumentation 測試。dependencies 中的第一行包含 JUnit 測試框架,第二行包含執行測試用例的測試執行器庫,最後第三行包含 Espresso 測試框架。

預設情況下,Android Studio 在建立 Android 專案時將 Espresso 測試框架(Android Support 庫)設定為依賴項,Gradle 將從 Maven 倉庫下載必要的庫。讓我們建立一個簡單的 Hello World Android 應用程式,並檢查 Espresso 測試框架是否已正確配置。

建立新的 Android 應用程式的步驟如下:

  • 啟動 Android Studio。

  • 選擇 檔案 → 新建 → 新建專案。

  • 輸入 應用程式名稱 (HelloWorldApp) 和公司域名 (espressosamples.tutorialspoint.com),然後單擊 下一步

Android Application

建立 Android 專案:

  • 選擇最低 API 為 API 15:Android 4.0.3 (IceCreamSandwich),然後單擊下一步。

Target Android Devices

選擇目標 Android 裝置:

  • 選擇 空 Activity,然後單擊 下一步

Empty Activity

向手機新增 Activity:

  • 輸入主 Activity 的名稱,然後單擊 完成

Main Activity

配置 Activity:

  • 建立新專案後,開啟 app/build.gradle 檔案並檢查其內容。檔案內容如下所示:

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.tutorialspoint.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'
}

最後一行指定了 Espresso 測試框架依賴項。預設情況下,配置了 Android Support 庫。我們可以透過單擊選單中的 重構遷移到 AndroidX 來重新配置應用程式以使用 AndroidX 庫。

Espresso Testing Framework

遷移到 AndroidX:

  • 現在,app/build.gradle 更改如下所示:

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.tutorialspoint.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 庫的 Espresso 測試框架。

裝置設定

在測試期間,建議關閉 Android 裝置上的動畫,該動畫用於測試。這將減少檢查空閒資源時的混淆。

讓我們看看如何在 Android 裝置上停用動畫 – (設定 → 開發者選項),

  • 視窗動畫縮放比例

  • 過渡動畫縮放比例

  • 動畫時長縮放比例

如果 設定 螢幕中沒有 開發者選項 選單,則多次點選 關於手機 選項中的 版本號。這將啟用 開發者選項 選單。

廣告
© . All rights reserved.