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

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

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

הוספה של ספריות בדיקה של AndroidX

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

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

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

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

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

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

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

אם באפליקציה נעשה שימוש בבדיקות שמסתמכות על 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" />