สร้างการทดสอบแบบมีเครื่องควบคุม

การทดสอบที่มีการใช้เครื่องมือวัดจะทำงานบนอุปกรณ์ Android ไม่ว่าจะเป็นอุปกรณ์จริงหรืออุปกรณ์จำลอง ด้วยเหตุนี้ แอปจึงใช้ประโยชน์จาก API ของเฟรมเวิร์ก Android ได้ ดังนั้นการทดสอบที่มีการใช้เครื่องมือจึงมีความแม่นยำมากกว่าการทดสอบในเครื่อง แม้ว่าจะทำงานช้ากว่ามากก็ตาม

เราขอแนะนำให้ใช้การทดสอบการวัดคุมเฉพาะในกรณีที่คุณต้องทดสอบกับลักษณะการทำงานของอุปกรณ์จริงเท่านั้น AndroidX Test มีไลบรารีหลายรายการที่ช่วยให้เขียนการทดสอบการวัดคุมได้ง่ายขึ้นเมื่อจำเป็น

ตั้งค่าสภาพแวดล้อมการทดสอบ

ในโปรเจ็กต์ Android Studio คุณจะจัดเก็บไฟล์ต้นฉบับสำหรับการทดสอบที่มีการวัดคุมไว้ใน module-name/src/androidTest/java/ ไดเรกทอรีนี้มีอยู่แล้วเมื่อคุณสร้างโปรเจ็กต์ใหม่และมีการทดสอบการวัดคุมตัวอย่าง

ก่อนเริ่มต้น คุณควรเพิ่ม AndroidX Test API ซึ่งช่วยให้คุณสร้างและเรียกใช้โค้ดการทดสอบการวัดคุมสำหรับแอปได้อย่างรวดเร็ว AndroidX Test ประกอบด้วยโปรแกรมเรียกใช้การทดสอบ JUnit 4, AndroidJUnitRunner และ API สำหรับการทดสอบ UI เชิงฟังก์ชัน เช่น Espresso, UI Automator และการทดสอบ Compose

นอกจากนี้ คุณยังต้องกำหนดค่าทรัพยากร Dependency ของการทดสอบ Android สำหรับโปรเจ็กต์เพื่อใช้ Test Runner และ API กฎที่ AndroidX Test มีให้ด้วย

ในไฟล์ build.gradle ระดับบนสุดของแอป คุณต้องระบุไลบรารีเหล่านี้เป็นทรัพยากร Dependency ดังนี้

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 เป็น Test Instrumentation Runner เริ่มต้นในโปรเจ็กต์โดยรวมการตั้งค่าต่อไปนี้ไว้ในไฟล์ build.gradle ระดับโมดูลของแอป

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

สร้างคลาสการทดสอบการวัดคุม

คลาสการทดสอบการวัดคุมควรเป็นคลาสการทดสอบ JUnit 4 ที่คล้ายกับคลาสที่อธิบายไว้ในส่วนเกี่ยวกับวิธีสร้างการทดสอบในเครื่อง

หากต้องการสร้างคลาสทดสอบ JUnit 4 ที่ใช้เครื่องมือวัด ให้ระบุ AndroidJUnit4 เป็น Test Runner เริ่มต้น

ตัวอย่างต่อไปนี้แสดงวิธีเขียนการทดสอบการวัดคุมเพื่อยืนยันว่าอินเทอร์เฟซ 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 มักเป็นการทดสอบที่มีการวัดคุมซึ่งยืนยันลักษณะการทำงานที่ถูกต้องของ UI โดยใช้เฟรมเวิร์ก เช่น Espresso หรือ Compose Test ดูข้อมูลเพิ่มเติมได้ที่คู่มือการทดสอบ UI

ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้การทดสอบการวัดคุมได้ที่แหล่งข้อมูลต่อไปนี้

ตัวอย่าง