AndroidX Test के लिए प्रोजेक्ट सेट अप करना

AndroidX Test, Jetpack लाइब्रेरी का कलेक्शन है. इसकी मदद से, Android ऐप्लिकेशन के ख़िलाफ़ टेस्ट चलाए जा सकते हैं. यह टेस्ट लिखने में आपकी मदद करने के लिए, कई टूल भी उपलब्ध कराता है.

उदाहरण के लिए, AndroidX Test, JUnit4 टेस्ट में गतिविधियां शुरू करने और उनसे इंटरैक्ट करने के लिए, JUnit4 के नियम उपलब्ध कराता है. इसमें यूज़र इंटरफ़ेस (यूआई) की टेस्टिंग के फ़्रेमवर्क भी शामिल हैं. जैसे, Espresso, UI Automator, और Robolectric सिम्युलेटर.

AndroidX Test लाइब्रेरी जोड़ना

AndroidX Test का इस्तेमाल करने के लिए, आपको अपने डेवलपमेंट एनवायरमेंट में, ऐप्लिकेशन प्रोजेक्ट की डिपेंडेंसी में बदलाव करना होगा.

ग्रेडल डिपेंडेंसी जोड़ना

अपने ऐप्लिकेशन प्रोजेक्ट की डिपेंडेंसी में बदलाव करने के लिए, यह तरीका अपनाएं:

  • पहला चरण: अपने ग्रेडल मॉड्यूल के लिए, build.gradle फ़ाइल खोलें.
  • दूसरा चरण: रिपॉज़िटरी सेक्शन में, पक्का करें कि Google की Maven रिपॉज़िटरी दिखे:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • तीसरा चरण: AndroidX Test के जिस पैकेज का इस्तेमाल करना है उसके लिए, डिपेंडेंसी सेक्शन में उसका पैकेज का नाम जोड़ें. उदाहरण के लिए, espresso-core पैकेज जोड़ने के लिए, यह लाइनें जोड़ें:

शानदार

dependencies {
        ...
        androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    }

Kotlin

dependencies {
        ...
        androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')
    }

AndroidX Test की ये सबसे आम डिपेंडेंसी उपलब्ध हैं:

शानदार

dependencies {
    // Core library
    androidTestImplementation "androidx.test:core:$androidXTestVersion0"

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation "androidx.test:runner:$testRunnerVersion"
    androidTestImplementation "androidx.test:rules:$testRulesVersion"

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
    androidTestImplementation "androidx.test.ext:truth:$truthVersion"

    // Espresso dependencies
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK’"s compile classpath or the test APK
    // classpath.
    androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
}

Kotlin

dependencies {
    // Core library
    androidTestImplementation("androidx.test:core:$androidXTestVersion")

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation("androidx.test:runner:$testRunnerVersion")
    androidTestImplementation("androidx.test:rules:$testRulesVersion")

    // Assertions
    androidTestImplementation("androidx.test.ext:junit:$testJunitVersion")
    androidTestImplementation("androidx.test.ext:truth:$truthVersion")

    // Espresso dependencies
    androidTestImplementation( "androidx.test.espresso:espresso-core:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-contrib:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-intents:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-accessibility:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-web:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso.idling:idling-concurrent:$espressoVersion")

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK"s compile classpath or the test APK
    // classpath.
    androidTestImplementation( "androidx.test.espresso:espresso-idling-resource:$espressoVersion")
}

रिलीज़ नोट वाले पेज पर, हर आर्टफ़ैक्ट के लिए सबसे नए वर्शन वाली टेबल मौजूद होती है.

इन लाइब्रेरी के बारे में खास रेफ़रंस दस्तावेज़ के लिए, पैकेज इंडेक्स या क्लास इंडेक्स देखें.

रोकी गई क्लास का इस्तेमाल करने वाले प्रोजेक्ट

अगर आपका ऐप्लिकेशन, JUnit3 पर आधारित android.test क्लास पर निर्भर रहने वाले टेस्ट का इस्तेमाल करता है, तो फ़ाइल के android सेक्शन में ये लाइनें जोड़ें. जैसे, InstrumentationTestCase और TestSuiteLoader:

android {
    ...
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
  }

मेनिफ़ेस्ट में एलान जोड़ना

JUnit3 पर आधारित android.test क्लास पर निर्भर रहने वाले टेस्ट चलाने के लिए, ज़रूरी <uses-library> एलिमेंट जोड़ें अपने टेस्ट ऐप्लिकेशन के मेनिफ़ेस्ट में. उदाहरण के लिए, अगर आपने ऐसे टेस्ट जोड़े हैं जो android.test.runner लाइब्रेरी पर निर्भर हैं, तो अपने ऐप्लिकेशन के मेनिफ़ेस्ट में यह एलिमेंट जोड़ें:

<!-- You don't need to include android:required="false" if your app's

   minSdkVersion is 28 or higher. -->

<uses-library android:name="android.test.runner"

       android:required="false" />

JUnit पर आधारित किसी क्लास को शामिल करने वाली लाइब्रेरी का पता लगाने के लिए, JUnit पर आधारित लाइब्रेरी देखें.

रोकी गई क्लास का इस्तेमाल करते समय और Android 9 या

इससे नए वर्शन को टारगेट करते समय ध्यान रखने वाली बातें

इस सेक्शन में दिया गया दिशा-निर्देश सिर्फ़ तब लागू होता है, जब आपका ऐप्लिकेशन Android 9 (एपीआई लेवल 28) या इससे नए वर्शन को टारगेट करता है और आपके ऐप्लिकेशन के लिए, कम से कम SDK वर्शन Android 9 पर सेट हो.

android.test.runner लाइब्रेरी, android.test.base और android.test.mock लाइब्रेरी पर निर्भर करती है. अगर आपका ऐप्लिकेशन सिर्फ़ android.test.base या android.test.mock की क्लास का इस्तेमाल करता है, तो इन लाइब्रेरी को अलग से शामिल किया जा सकता है:

<!-- For both of these declarations, you don't need to include
   android:required="false" if your app's minSdkVersion is 28
   or higher. -->

<uses-library android:name="android.test.base"
       android:required="false" />
<uses-library android:name="android.test.mock"
       android:required="false" />