הגדרת פרויקט לבדיקת AndroidX

‫AndroidX Test הוא אוסף של ספריות Jetpack שמאפשרות להריץ בדיקות באפליקציות ל-Android. הוא גם מספק סדרה של כלים שיעזרו לכם לכתוב את הבדיקות האלה.

לדוגמה, AndroidX Test מספק כללים של JUnit4 להפעלת פעילויות וליצירת אינטראקציה איתן בבדיקות JUnit4. הוא כולל גם מסגרות לבדיקת ממשק משתמש, כמו Espresso,‏ UI Automator והסימולטור Robolectric.

הוספת ספריות של AndroidX Test

כדי להשתמש ב-AndroidX Test, צריך לשנות את התלות של פרויקט האפליקציה בסביבת הפיתוח.

הוספת תלויות של Gradle

כדי לשנות את התלות של פרויקט האפליקציה, מבצעים את השלבים הבאים:

  • שלב 1: פותחים את הקובץ build.gradle של מודול Gradle.
  • שלב 2: בקטע repositories, מוודאים שמאגר Maven של Google מופיע:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • שלב 3: לכל חבילת 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")
}

בדף הערות הגרסה מופיעה טבלה עם הגרסאות האחרונות של כל ארטיפקט.

אפשר לעיין באינדקס החבילות או באינדקס המחלקות כדי לקבל תיעוד ספציפי של הספריות האלה.

פרויקטים שנעשה בהם שימוש במחלקות שהוצאו משימוש

אם האפליקציה שלך משתמשת בבדיקות שמסתמכות על מחלקות android.test מבוססות JUnit3 שיצאו משימוש , כמו InstrumentationTestCase ו-TestSuiteLoader, צריך להוסיף את השורות הבאות בקטע android של הקובץ:

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

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

הוספת הצהרות במניפסט

כדי להריץ בדיקות שמסתמכות על מחלקות android.test מבוססות JUnit3 שהוצאו משימוש, צריך להוסיף את רכיבי <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 (רמת API‏ 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" />