AndroidX Test là một tập hợp các thư viện Jetpack cho phép bạn chạy các kiểm thử đối với ứng dụng Android. Khung này cũng cung cấp một loạt công cụ giúp bạn viết các kiểm thử này.
Ví dụ: AndroidX Test cung cấp các quy tắc JUnit4 để bắt đầu các hoạt động và tương tác với các hoạt động đó trong các kiểm thử JUnit4. SDK này cũng chứa các khung Kiểm thử giao diện người dùng như Espresso, UI Automator và trình mô phỏng Robolectric.
Thêm thư viện kiểm thử AndroidX
Để sử dụng AndroidX Test, bạn phải sửa đổi các phần phụ thuộc của dự án ứng dụng trong môi trường phát triển.
Thêm phần phụ thuộc vào Gradle
Để sửa đổi các phần phụ thuộc của dự án ứng dụng, hãy hoàn tất các bước sau:
- Bước 1: Mở tệp
build.gradle
cho mô-đun Gradle của bạn. - Bước 2: Trong phần kho lưu trữ, hãy đảm bảo kho lưu trữ Maven của Google xuất hiện:
allprojects {
repositories {
jcenter()
google()
}
}
- Bước 3: Đối với mỗi gói Kiểm thử AndroidX mà bạn muốn sử dụng, hãy thêm tên gói của gói đó vào phần dependencies. Ví dụ: để thêm gói
espresso-core
, hãy thêm các dòng sau:
Groovy
dependencies { ... androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" }
Kotlin
dependencies { ... androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion') }
Sau đây là các phần phụ thuộc phổ biến nhất của 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") }
Trang Ghi chú phát hành chứa một bảng có các phiên bản mới nhất cho mỗi cấu phần phần mềm.
Tham khảo Chỉ mục gói hoặc Chỉ mục lớp để biết tài liệu tham khảo cụ thể về các thư viện này.
Các dự án sử dụng những lớp không dùng nữa
Nếu ứng dụng của bạn sử dụng các kiểm thử dựa trên các lớp android.test
dựa trên JUnit3 không dùng nữa , chẳng hạn như InstrumentationTestCase
và TestSuiteLoader
, hãy thêm các dòng sau vào phần android
của tệp:
android {
...
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
Thêm nội dung khai báo tệp kê khai
Để chạy các kiểm thử dựa trên các lớp android.test
dựa trên JUnit3 không dùng nữa, hãy thêm các phần tử <uses-library>
cần thiết vào tệp kê khai của ứng dụng kiểm thử. Ví dụ: nếu bạn thêm các kiểm thử phụ thuộc vào thư viện android.test.runner
, hãy thêm phần tử sau vào tệp kê khai của ứng dụng:
<!-- 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" />
Để xác định thư viện chứa một lớp dựa trên JUnit nhất định, hãy xem Thư viện dựa trên JUnit.
Những điều cần cân nhắc khi sử dụng các lớp không dùng nữa và nhắm đến Android 9 trở lên
cao hơn
Hướng dẫn trong phần này chỉ áp dụng nếu bạn nhắm đến Android 9 (API cấp 28) trở lên và phiên bản SDK tối thiểu cho ứng dụng của bạn được đặt thành Android 9.
Thư viện android.test.runner
phụ thuộc ngầm vào các thư viện android.test.base
và android.test.mock
. Nếu ứng dụng của bạn chỉ sử dụng các lớp từ android.test.base
hoặc android.test.mock
, thì bạn có thể tự mình thêm các thư viện:
<!-- 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" />