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

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

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

เพิ่มไลบรารีการทดสอบ AndroidX

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

เพิ่มการขึ้นต่อกันของ Gradle

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

  • ขั้นตอนที่ 1: เปิดไฟล์ build.gradle สำหรับโมดูล Gradle
  • ขั้นตอนที่ 2: ในส่วนที่เก็บ ให้ตรวจสอบว่าที่เก็บ 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'   } 

เพิ่มการประกาศไฟล์ 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" />