打造檢測設備測試

設備測試是在 Android 裝置上執行,無論是實體或模擬都沒問題。因此,這些開發人員可以運用 Android 架構 API。檢測設備測試因此的擬真度高於本機測試,但測試速度較慢。

建議您只在必須測試實際裝置行為的情況下才使用檢測設備測試。AndroidX Test 提供多個程式庫,方便您視需要編寫檢測設備測試。

設定測試環境

在 Android Studio 專案中,將檢測設備測試的來源檔案儲存在 module-name/src/androidTest/java/ 中。當您建立新專案,並含有檢測設備測試範例時,已有此目錄。

開始之前,您應新增 AndroidX Test API,以便快速為應用程式建構及執行檢測設備測試程式碼。AndroidX 測試提供 JUnit 4 測試執行器 AndroidJUnitRunner,以及可進行功能 UI 測試的 API,例如 EspressoUI AutomatorCompose test

您也必須設定專案的 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 的正確行為。這些 API 會使用 EspressoCompose Test 等架構。詳情請參閱 UI 測試指南

如要進一步瞭解如何使用檢測設備測試,請參閱下列資源。

範例

程式碼研究室