ตั้งค่าโปรเจ็กต์สำหรับการทดสอบ AndroidX

AndroidX Test คือคอลเล็กชันไลบรารี Jetpack ที่ให้คุณทำการทดสอบได้ กับแอป Android และยังมีชุดเครื่องมือเพื่อช่วยในการเขียนข้อความเหล่านี้ การทดสอบ

ตัวอย่างเช่น AndroidX Test มีกฎ JUnit4 เพื่อเริ่มกิจกรรมและ โต้ตอบกับผู้ใช้ในการทดสอบ JUnit4 นอกจากนี้ยังมีเฟรมเวิร์กการทดสอบ UI เช่น เป็น Espresso, UI Automator และเครื่องมือจำลอง Robolectric

เพิ่มไลบรารี AndroidX Test

คุณต้องแก้ไขทรัพยากร Dependency ของโปรเจ็กต์แอปเพื่อใช้ AndroidX Test ภายในสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณ

เพิ่มการอ้างอิง Gradle

หากต้องการแก้ไขทรัพยากร Dependency ของโปรเจ็กต์แอป ให้ทำตามขั้นตอนต่อไปนี้

  • ขั้นตอนที่ 1: เปิดไฟล์ build.gradle สำหรับโมดูล Gradle
  • ขั้นตอนที่ 2: ในส่วนที่เก็บ ให้ตรวจสอบว่า Maven ของ Google ที่เก็บจะปรากฏ:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • ขั้นตอนที่ 3: เพิ่มแพ็กเกจของแพ็กเกจ AndroidX Test แต่ละแพ็กเกจที่ต้องการใช้ เป็นชื่อ ทรัพยากร Dependency ตัวอย่างเช่น หากต้องการเพิ่มแพ็กเกจ espresso-core ให้เพิ่ม บรรทัดต่อไปนี้:

ดึงดูด

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

Kotlin

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

ทรัพยากร Dependency ทดสอบของ 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"
}

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

เพิ่มประกาศเกี่ยวกับไฟล์ Manifest

หากต้องการเรียกใช้การทดสอบที่ใช้คลาส android.test แบบ JUnit3 ที่เลิกใช้งานแล้ว ให้เพิ่ม องค์ประกอบ <uses-library> ที่จำเป็นในไฟล์ Manifest ของแอปทดสอบ สำหรับ ตัวอย่างเช่น หากคุณเพิ่มการทดสอบที่ขึ้นอยู่กับไลบรารี android.test.runner ให้เพิ่ม องค์ประกอบต่อไปนี้ลงในไฟล์ Manifest ของแอป

<!-- 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" />