راه‌اندازی پروژه برای AndroidX Test

‫AndroidX Test مجموعه‌ای از کتابخانه‌های Jetpack است که به شما امکان می‌دهد برنامه‌های Android را آزمایش کنید. این چارچوب همچنین مجموعه‌ای از ابزارها را برای کمک به شما در نوشتن این آزمایش‌ها ارائه می‌دهد.

برای مثال، AndroidX Test قوانین JUnit4 را برای شروع فعالیت‌ها و تعامل با آن‌ها در آزمایش‌های JUnit4 ارائه می‌دهد. همچنین شامل چارچوب‌های «آزمایش میانای کاربر» مانند Espresso،‏ UI Automator، و شبیه‌ساز Robolectric است.

افزودن کتابخانه‌های AndroidX Test

برای استفاده از AndroidX Test، باید وابستگی‌های پروژه برنامه‌تان را در محیط توسعه تغییر دهید.

افزودن وابستگی‌های Gradle

برای اصلاح وابستگی‌های پروژه برنامه، مراحل زیر را تکمیل کنید:

  • مرحله ۱: فایل build.gradle را برای واحد Gradle خود باز کنید.
  • مرحله ۲: در بخش مخزن‌ها، مطمئن شوید مخزن Maven مربوط به Google نمایش داده می‌شود:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • مرحله ۳: برای هر بسته AndroidX Test که می‌خواهید استفاده کنید، نام بسته آن را به بخش وابستگی‌ها اضافه کنید. برای مثال، برای افزودن بسته espresso-core، خطوط زیر را اضافه کنید:

شیک

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

کاتلین

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

این‌ها رایج‌ترین وابستگی‌های «آزمایش AndroidX» دردسترس هستند:

شیک

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"
}

کاتلین

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) یا بالاتر را هدف‌یابی کنید و حداقل نسخه کیت توسعه نرم‌افزار برای برنامه‌تان روی 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" />