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
, מוסיפים את השורות הבאות:
גרוב
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" />