פיתוח בדיקות במכשירים

בדיקות אינסטרומנטטיביות פועלות במכשירי Android, גם פיזיים וגם כבדיקות אמולציה. בתור ולכן הם יכולים לנצל את ממשקי ה-API של Android framework. מבחנים אינסטרומנטלים ולכן מספקות רמת דיוק גבוהה יותר מבדיקות מקומיות, למרות שהן פועלות הרבה יותר לאט.

מומלץ להשתמש בבדיקות מכשירים רק במקרים שבהם צריך לבצע בדיקה בהשוואה ההתנהגות של מכשיר אמיתי. יש מספר ספריות בכלי AndroidX Test שמקלות על כתיבת מבחנים אינסטרומנטליים במקרה הצורך.

הגדרת סביבת הבדיקה

בפרויקט Android Studio, אתם מאחסנים את קובצי המקור של בדיקות בmodule-name/src/androidTest/java/. הספרייה הזו כבר קיימת כאשר יוצרים פרויקט חדש ומכילים דוגמה לבדיקה עם אינסטרומנטציה.

לפני שמתחילים, יש להוסיף ממשקי API של AndroidX Test, שמאפשרים לבצע במהירות לפתח ולהריץ קוד בדיקה אינסטרומנטלי עבור האפליקציות. בדיקת AndroidX כוללת הרצת בדיקה של JUnit 4 ,AndroidJUnitRunner וממשקי API לבדיקות ממשק משתמש פונקציונליות כמו Espresso, UI Automator ו-Compose test.

צריך גם להגדיר את יחסי התלות של בדיקות Android עבור הפרויקט שלך כדי להשתמש בהרצת הבדיקה ובממשקי ה-API של הכללים שמסופקים על ידי AndroidX Test.

בקובץ 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 ובקטע כתיבה נתוני גרסה של ממשק המשתמש.

כדי להשתמש בכיתות בדיקה ב-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 תלמדו:

מקורות מידע נוספים

בדיקות ממשק משתמש הן בדרך כלל בדיקות אינסטרומנטליות שמאמתות את ההתנהגות הנכונה של ממשק המשתמש. הן משתמשות ב-frameworks כמו Espresso או Compose Test. למידה עוד, קראו את המדריך לבדיקת ממשק המשתמש.

לקבלת מידע נוסף על השימוש בבדיקות האינסטרומנטציה, אפשר לעיין במאמרים הבאים המשאבים.

דוגמה

שיעורי Lab