檢測設備測試會在 Android 裝置上執行,無論是實體或模擬裝置。因此,他們可以利用 Android 架構 API。因此,檢測設備測試的擬真度比本機測試高,但執行速度慢得多。
建議您只在必須測試實際裝置行為時,才使用檢測設備測試。AndroidX Test 提供多個程式庫,可讓您在必要時更輕鬆地編寫檢測設備測試。
設定測試環境
在 Android Studio 專案中,檢測設備測試的來源檔案會儲存在 module-name/src/androidTest/java/。建立新專案時,這個目錄已存在,且包含檢測設備測試範例。
開始之前,請先新增 AndroidX Test API,以便快速建構及執行應用程式的檢測設備測試程式碼。AndroidX Test 包含 JUnit 4 測試執行器 AndroidJUnitRunner,以及 Espresso、UI Automator 和 Compose 測試等功能性 UI 測試的 API。
您也需要為專案設定 Android 測試依附元件,才能使用 AndroidX Test 提供的測試執行工具和規則 API。
在應用程式的頂層 build.gradle 檔案中,您需要將這些程式庫指定為依附元件:
dependencies {
androidTestImplementation "androidx.test:runner:$androidXTestVersion"
androidTestImplementation "androidx.test:rules:$androidXTestVersion"
// Optional -- UI testing with Espresso
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
// Optional -- UI testing with UI Automator
androidTestImplementation "androidx.test.uiautomator:uiautomator:$uiAutomatorVersion"
// Optional -- UI testing with Compose
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}
您可以在 AndroidX 版本資訊和 Compose UI 版本資訊中找到最新版本。
如要使用 JUnit 4 測試類別,並存取測試篩選等功能,請務必在應用程式的模組層級 build.gradle 檔案中加入下列設定,將 AndroidJUnitRunner 指定為專案中的預設測試檢測執行工具:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
建立檢測設備測試類別
檢測設備測試類別應為 JUnit 4 測試類別,類似於「建構本機測試」一節所述的類別。
如要建立檢測設備 JUnit 4 測試類別,請將 AndroidJUnit4 指定為預設測試執行器。
以下範例說明如何編寫檢測設備測試,驗證 LogHistory 類別是否正確實作 Parcelable 介面:
Kotlin
import android.os.Parcel import android.text.TextUtils.writeToParcel import androidx.test.filters.SmallTest import androidx.test.runner.AndroidJUnit4 import com.google.common.truth.Truth.assertThat import org.junit.Before import org.junit.Test import org.junit.runner.RunWith const val TEST_STRING = "This is a string" const val TEST_LONG = 12345678L // @RunWith is required only if you use a mix of JUnit3 and JUnit4. @RunWith(AndroidJUnit4::class) @SmallTest class LogHistoryAndroidUnitTest { private lateinit var logHistory: LogHistory @Before fun createLogHistory() { logHistory = LogHistory() } @Test fun logHistory_ParcelableWriteRead() { val parcel = Parcel.obtain() logHistory.apply { // Set up the Parcelable object to send and receive. addEntry(TEST_STRING, TEST_LONG) // Write the data. writeToParcel(parcel, describeContents()) } // After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0) // Read the data. val createdFromParcel: LogHistory = LogHistory.CREATOR.createFromParcel(parcel) createdFromParcel.getData().also { createdFromParcelData: List<Pair<String, Long>> -> // Verify that the received data is correct. assertThat(createdFromParcelData.size).isEqualTo(1) assertThat(createdFromParcelData[0].first).isEqualTo(TEST_STRING) assertThat(createdFromParcelData[0].second).isEqualTo(TEST_LONG) } } }
Java
import android.os.Parcel; import android.util.Pair; import androidx.test.runner.AndroidJUnit4; import com.google.common.truth.Truth.assertThat; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // @RunWith is required only if you use a mix of JUnit3 and JUnit4. @RunWith(AndroidJUnit4.class) public class LogHistoryAndroidUnitTest { public static final String TEST_STRING = "This is a string"; public static final long TEST_LONG = 12345678L; private LogHistory mLogHistory; @Before public void createLogHistory() { mLogHistory = new LogHistory(); } @Test public void logHistory_ParcelableWriteRead() { // Set up the Parcelable object to send and receive. mLogHistory.addEntry(TEST_STRING, TEST_LONG); // Write the data. Parcel parcel = Parcel.obtain(); mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); // After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0); // Read the data. LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel); List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); // Verify that the received data is correct. assertThat(createdFromParcelData.size()).isEqualTo(1); assertThat(createdFromParcelData.get(0).first).isEqualTo(TEST_STRING); assertThat(createdFromParcelData.get(0).second).isEqaulTo(TEST_LONG); } }
執行檢測設備測試
檢測設備測試可在實體裝置或模擬器上執行。Android Studio 指南說明如何:
其他資源
UI 測試通常是檢測設備測試,用於驗證 UI 的行為是否正確。這類測試會使用 Espresso 或 Compose Test 等架構。詳情請參閱 UI 測試指南。
如要進一步瞭解如何使用檢測設備測試,請參閱下列資源。