設備測試會在 Android 裝置上執行,無論是實體或模擬裝置。因此可以利用 Android 架構 API。因此,設備測試的執行速度比本機測試還要慢,但精確度更高。
建議您只有在必須測試實際裝置行為時,才使用檢測設備測試。AndroidX Test 提供多種程式庫,方便您在必要時撰寫檢測設備測試。
設定測試環境
在 Android Studio 專案中,您可以將檢測設備測試的來源檔案儲存在 module-name/src/androidTest/java/
中。當您建立新專案,且其中包含範例檢測設備測試時,便已存在這個目錄。
在開始之前,您應新增 AndroidX Test API,可讓您快速為應用程式建構及執行檢測設備測試程式碼。AndroidX 測試包含 JUnit 4 測試執行器、AndroidJUnitRunner
,以及功能 UI 測試 (例如 Espresso、UI Automator 和 Compose 測試) 的 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 測試指南。
如要進一步瞭解如何使用檢測設備測試,請參閱下列資源。