AndroidX Test คือคอลเล็กชันของไลบรารี Jetpack ที่ช่วยให้คุณเรียกใช้การทดสอบกับแอป Android ได้ นอกจากนี้ ยังมีชุดเครื่องมือที่จะช่วยคุณเขียนการทดสอบเหล่านี้ด้วย
เช่น AndroidX Test มีกฎ JUnit4 เพื่อเริ่มกิจกรรมและ โต้ตอบกับกิจกรรมเหล่านั้นในการทดสอบ JUnit4 นอกจากนี้ ยังมีเฟรมเวิร์กการทดสอบ UI เช่น Espresso, UI Automator และโปรแกรมจำลอง Robolectric
เพิ่มไลบรารีการทดสอบ AndroidX
หากต้องการใช้ AndroidX Test คุณต้องแก้ไขทรัพยากร Dependency ของโปรเจ็กต์แอป ภายในสภาพแวดล้อมในการพัฒนาซอฟต์แวร์
เพิ่มการขึ้นต่อกันของ Gradle
หากต้องการแก้ไขการอ้างอิงของโปรเจ็กต์แอป ให้ทำตามขั้นตอนต่อไปนี้
- ขั้นตอนที่ 1: เปิดไฟล์
build.gradleสำหรับโมดูล Gradle - ขั้นตอนที่ 2: ในส่วนที่เก็บ ให้ตรวจสอบว่าที่เก็บ 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'
}
เพิ่มการประกาศไฟล์ 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" />