इंस्ट्रुमेंट्ड टेस्ट तैयार करें

इंस्ट्रुमेंटेड टेस्ट, Android डिवाइसों पर चलाए जाते हैं. भले ही, वे फ़िज़िकल डिवाइस हों या एम्युलेट किए गए डिवाइस. इसलिए, वे Android फ़्रेमवर्क एपीआई का फ़ायदा ले सकते हैं. इसलिए, इंस्ट्रुमेंटेड टेस्ट, लोकल टेस्ट की तुलना में ज़्यादा सटीक नतीजे देते हैं. हालांकि, ये टेस्ट बहुत धीमी गति से चलते हैं.

हमारा सुझाव है कि इंस्ट्रुमेंटेड टेस्ट का इस्तेमाल सिर्फ़ तब करें, जब आपको किसी असली डिवाइस के व्यवहार के हिसाब से टेस्ट करना हो. AndroidX Test कई लाइब्रेरी उपलब्ध कराता है. इनकी मदद से, ज़रूरत पड़ने पर इंस्ट्रुमेंटेड टेस्ट लिखना आसान हो जाता है.

अपना टेस्टिंग एनवायरमेंट सेट अप करना

अपने Android Studio प्रोजेक्ट में, इंस्ट्रुमेंटेड टेस्ट के लिए सोर्स फ़ाइलों को module-name/src/androidTest/java/ में सेव किया जाता है. नया प्रोजेक्ट बनाने पर, यह डायरेक्ट्री पहले से मौजूद होती है. इसमें इंस्ट्रुमेंटेड टेस्ट का एक उदाहरण शामिल होता है.

शुरू करने से पहले, आपको AndroidX Test API जोड़ने चाहिए. इनकी मदद से, अपने ऐप्लिकेशन के लिए इंस्ट्रुमेंटेड टेस्ट कोड को तुरंत बनाया और चलाया जा सकता है. AndroidX Test में JUnit 4 टेस्ट रनर ,AndroidJUnitRunner, और फ़ंक्शनल यूज़र इंटरफ़ेस (यूआई) टेस्ट के लिए एपीआई शामिल हैं. जैसे, Espresso, UI Automator, और Compose test.

आपको अपने प्रोजेक्ट के लिए, Android टेस्टिंग डिपेंडेंसी भी कॉन्फ़िगर करनी होंगी. इससे, 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 के रिलीज़ नोट्स और Compose यूज़र इंटरफ़ेस (यूआई) के रिलीज़ नोट्स में नए वर्शन मिल सकते हैं.

JUnit 4 टेस्ट क्लास का इस्तेमाल करने और टेस्ट फ़िल्टर करने जैसी सुविधाओं को ऐक्सेस करने के लिए, अपने प्रोजेक्ट में AndroidJUnitRunner को डिफ़ॉल्ट टेस्ट इंस्ट्रुमेंटेशन रनर के तौर पर सेट करें. इसके लिए, अपने ऐप्लिकेशन की मॉड्यूल-लेवल build.gradle फ़ाइल में यह सेटिंग शामिल करें:

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 की गाइड में, आपको ये चीज़ें सीखने को मिलेंगी:

अन्य संसाधन

यूज़र इंटरफ़ेस (यूआई) टेस्ट आम तौर पर इंस्ट्रुमेंटेड टेस्ट होते हैं. इनसे यूज़र इंटरफ़ेस (यूआई) के सही तरीके से काम करने की पुष्टि की जाती है. वे Espresso या Compose Test जैसे फ़्रेमवर्क का इस्तेमाल करते हैं. ज़्यादा जानने के लिए, यूज़र इंटरफ़ेस (यूआई) की जांच करने से जुड़ी गाइड पढ़ें.

इंस्ट्रुमेंटेशन टेस्ट इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, यहां दिए गए संसाधन देखें.

नमूना