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

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

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

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

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

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

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

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

Groovy

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

Kotlin

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

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

Groovy

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 क्लास पर निर्भर करते हैं , जैसे कि InstrumentationTestCase और TestSuiteLoader, तो फ़ाइल के android सेक्शन में ये लाइनें जोड़ें:

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) या इससे बाद के वर्शन को टारगेट करता हो और आपके ऐप्लिकेशन के लिए कम से कम एसडीके वर्शन, 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" />