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

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

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

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

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

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

आपको अपने प्रोजेक्ट के लिए, Android टेस्टिंग डिपेंडेंसी को भी कॉन्फ़िगर करना होगा: टेस्ट रनर और AndroidX टेस्ट के नियमों वाले एपीआई का इस्तेमाल करना होगा.

अपने ऐप्लिकेशन की टॉप-लेवल की 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 को अपने डिफ़ॉल्ट टेस्ट रनर.

नीचे दिया गया उदाहरण दिखाता है कि पुष्टि करने के लिए, इंस्ट्रुमेंटेड टेस्ट कैसे लिखा जा सकता है कि पार्स किए जा सकने वाले इंटरफ़ेस को इसके लिए सही तरीके से लागू किया गया है: 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 में स्टूडियो गाइड में ये काम करने के तरीके बताए गए हैं:

अन्य संसाधन

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

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

नमूना

कोड लैब