AndroidX Test là một tập hợp gồm các thư viện Jetpack cho phép bạn chạy kiểm thử trên các ứng dụng Android. Hướng dẫn này cũng cung cấp một loạt công cụ giúp bạn viết các chương trình 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 chúng trong các thử nghiệm JUnit4. Thư viện này cũng chứa các khung Kiểm thử giao diện người dùng như Espresso, Automator giao diện người dùng 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 mục phần phụ thuộc. 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') }
Dưới đây là các phần phụ thuộc AndroidX Test phổ biến nhất hiện có:
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 Package Index (Chỉ mục gói) hoặc Class Index (Chỉ mục lớp) để xem tài liệu tham khảo cụ thể về các thư viện này.
Dự án sử dụng các lớp không dùng nữa
Nếu ứng dụng của bạn dùng các chương trình kiểm thử dựa vào 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 trong tệp kê khai
Để chạy kiểm thử dựa vào 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 bài kiểm thử phụ thuộc vào thư viện android.test.runner
, hãy thêm phần tử sau đây 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 các 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 hoặc
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 đặt là Android 9.
Thư viện android.test.runner
ngầm phụ thuộc 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ự đưa các thư viện vào:
<!-- 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" />