打造檢測設備測試

設備測試會在 Android 裝置上執行,包括實體或模擬。阿斯 以便運用 Android 架構 API檢測設備測試 因此準確度會比本機測試更高 。

建議您只在必須進行測試時,才使用檢測設備測試 模擬真實裝置的行為AndroidX Test 提供多個程式庫 可讓您在必要時更輕鬆地編寫檢測設備測試

設定測試環境

在 Android Studio 專案中,儲存用於檢測的來源檔案 module-name/src/androidTest/java/ 中的測試。這個目錄已存在 請先建立一項新專案,並加入檢測設備測試範例

開始之前,您應新增 AndroidX Test API,以便快速 為應用程式建立並執行檢測設備測試程式碼。AndroidX Test 內含 JUnit 4 測試執行器、AndroidJUnitRunner 和用於功能 UI 測試的 API ,例如 EspressoUI AutomatorCompose 測試

您也需要為專案設定 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 測試類別並存取測試篩選等功能, 請務必將 AndroidJUnitRunner 指定為預設的測試檢測設備 將下列設定加入應用程式的 模組層級 build.gradle 檔案:

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

建立檢測設備測試類別

您的檢測設備測試類別應是與以下類似的 JUnit 4 測試類別: 請參閱建構本機測試一節所述的類別。

如要建立檢測 JUnit 4 測試類別,請將 AndroidJUnit4 指定為 預設測試執行器

以下範例說明如何編寫檢測設備測試以進行驗證 Parcelable 介面正確導入, LogHistory 類別:

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 測試通常是檢測設備測試,可驗證 以及如何透過使用者介面他們會使用 EspressoCompose Test 等架構。學習 詳情請參閱 UI 測試指南

如要進一步瞭解如何使用檢測設備測試,請參閱下列資源 再複習一下,機構節點 是所有 Google Cloud Platform 資源的根節點

範例

程式碼研究室