Bộ kiểm thử đo lường đóng gói các hoạt động kiểm thử thiết bị thành các tệp APK testApk riêng biệt bên ngoài thư mục src/androidTest/ mặc định của mô-đun. Định cấu hình các chương trình kiểm thử này bằng DSL bộ kiểm thử của trình bổ trợ Android cho Gradle (AGP) để tách riêng các quy trình kiểm tra giao diện người dùng, tích hợp hoặc toàn diện bằng các phần phụ thuộc, biến thể và mục tiêu thiết bị chuyên biệt.
Yêu cầu
Để sử dụng bộ kiểm thử đo lường, hãy đảm bảo rằng dự án của bạn đáp ứng các yêu cầu sau:
- Android Studio Rabbit 3 Canary 3 trở lên
- AGP 9.6.0-alpha03 trở lên
- Gradle 9.7.1 trở lên và JDK 17 trở lên
- Mô-đun
com.android.applicationhoặccom.android.library - Một thiết bị hoặc trình mô phỏng được kết nối thông qua Cầu gỡ lỗi Android (adb) hoặc một định nghĩa thiết bị do bản dựng quản lý
Để biết thêm thông tin về việc so khớp phiên bản xem trước, hãy xem phần Khả năng tương thích với các bản xem trước của trình bổ trợ Android cho Gradle.
Thiết lập bộ kiểm thử đo lường
Để thêm bộ kiểm thử đo lường vào một ứng dụng hoặc mô-đun thư viện, hãy hoàn tất các bước sau:
Bật bộ kiểm thử
Trong tệp gradle.properties của dự án, hãy bật tính năng hỗ trợ bộ kiểm thử:
android.experimental.testSuiteSupport=true
Định cấu hình bộ ứng dụng
Trong tệp build.gradle.kts của mô-đun, hãy tạo một bộ kiểm thử bên trong khối testOptions.customSuites có các khối testApk và useJunitEngine cũng như các giá trị enum AgpTestSuiteInputParameters:
import com.android.build.api.dsl.AgpTestSuiteInputParameters
android {
testOptions {
// Optional: define build-managed virtual devices and device groups.
managedDevices {
localDevices {
create("api34Atd") {
// Use a device profile from Android Studio.
device = "Pixel 6"
apiLevel = 34
systemImageSource = "aosp-atd"
}
}
groups {
create("ciPhones") {
targetDevices.add(allDevices.getByName("api34Atd"))
}
}
}
customSuites {
create("androidSuite") {
targetVariants += listOf("debug")
testApk {
dependencies {
implementation("junit:junit:4.13.2")
implementation("androidx.test.ext:junit:1.2.1")
implementation("androidx.test:runner:1.6.2")
}
}
useJunitEngine {
includeEngines += listOf("android-test-engine")
// Match the engine and listener versions to your AGP
// version.
enginesDependencies.add(
"com.android.tools.androidtest:" +
"android-test-engine:9.6.0-alpha03"
)
enginesDependencies.add(
"com.android.tools.androidtest:" +
"android-test-engine-result-listener:9.6.0-alpha03"
)
enginesDependencies.add(
"org.junit.platform:junit-platform-launcher:1.10.2"
)
inputs += listOf(
// Omit TESTED_APKS in com.android.library modules.
AgpTestSuiteInputParameters.TESTED_APKS,
AgpTestSuiteInputParameters.TEST_APKS,
AgpTestSuiteInputParameters.ADB_EXECUTABLE,
AgpTestSuiteInputParameters.AAPT2_EXECUTABLE,
)
}
targets {
// Runs on connected adb devices.
create("default")
// Optional: runs on build-managed devices or groups in
// targetDevices.
create("managed") {
targetDevices += listOf("ciPhones")
}
}
}
}
}
}
AGP tự động định cấu hình trình chạy kiểm thử AndroidJUnitRunner và mã nhận dạng gói mà không kế thừa các phần phụ thuộc từ cấu hình androidTestImplementation. Để sử dụng trình chạy tuỳ chỉnh, hãy đặt thuộc tính android:name trên phần tử instrumentation trong tệp src/{suiteName}/AndroidManifest.xml.
Tạo các bài kiểm thử
Đặt kiểm thử đo lường vào thư mục {module}/src/{suiteName}/kotlin/ hoặc java/, cùng với mọi thư mục res/ hoặc assets/ không bắt buộc hoặc tệp AndroidManifest.xml. Ví dụ: tạo tệp app/src/androidSuite/kotlin/com/example/myapp/AppContextSuiteTest.kt:
package com.example.myapp
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class AppContextSuiteTest {
@Test
fun useAppContext() {
val appContext =
InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.myapp", appContext.packageName)
}
}
Kết hợp kiểm thử đơn vị và kiểm thử đo lường
Để chạy kiểm thử JVM cục bộ và kiểm thử đo lường thiết bị trong một tác vụ Gradle, hãy khai báo cả khối hostJar và testApk trong một bộ kiểm thử. Đặt các bài kiểm thử máy chủ lưu trữ trong thư mục src/{suiteName}/test/ và các bài kiểm thử thiết bị trong thư mục src/{suiteName}/androidTest/:
import com.android.build.api.dsl.AgpTestSuiteInputParameters
android {
testOptions {
customSuites {
create("combinedSuite") {
targetVariants += listOf("debug")
hostJar {
dependencies {
implementation(
"org.junit.jupiter:junit-jupiter-api:5.10.2"
)
}
}
testApk {
dependencies {
implementation("junit:junit:4.13.2")
implementation("androidx.test.ext:junit:1.2.1")
implementation("androidx.test:runner:1.6.2")
}
}
useJunitEngine {
includeEngines += listOf(
"junit-jupiter",
"android-test-engine",
)
enginesDependencies.add(
"org.junit.jupiter:junit-jupiter-engine:5.10.2"
)
enginesDependencies.add(
"com.android.tools.androidtest:" +
"android-test-engine:9.6.0-alpha03"
)
enginesDependencies.add(
"com.android.tools.androidtest:" +
"android-test-engine-result-listener:9.6.0-alpha03"
)
enginesDependencies.add(
"org.junit.platform:junit-platform-launcher:1.10.2"
)
inputs += listOf(
// Omit TESTED_APKS in com.android.library modules.
AgpTestSuiteInputParameters.TESTED_APKS,
AgpTestSuiteInputParameters.TEST_APKS,
AgpTestSuiteInputParameters.ADB_EXECUTABLE,
AgpTestSuiteInputParameters.AAPT2_EXECUTABLE,
)
}
targets {
create("default")
}
}
}
}
}
Chạy kiểm thử và thu thập mức độ sử dụng
Sử dụng Gradle để chạy các bộ kiểm thử đo lường, lọc các trường hợp kiểm thử và tạo báo cáo mức độ phù hợp.
Chạy trên thiết bị đã kết nối hoặc thiết bị do bản dựng quản lý
Chạy một mục tiêu đã kết nối trên mọi thiết bị adb trực tuyến hoặc chạy một mục tiêu được định cấu hình bằng thuộc tính targetDevices:
# Run on connected adb devices. ./gradlew :app:testDebugAndroidSuiteDefaultTestSuite# Run on the ciPhones build-managed device group. ./gradlew :app:testDebugAndroidSuiteManagedCiPhonesTestSuite
Lọc kiểm thử đo lường
Lọc kiểm thử bằng tuỳ chọn --tests hoặc đối số AndroidJUnitRunner của Gradle:
./gradlew :app:testDebugAndroidSuiteDefaultTestSuite \
--tests "com.example.myapp.AppContextSuiteTest.useAppContext"Bạn cũng có thể truyền các đối số AndroidJUnitRunner trên dòng lệnh bằng một thuộc tính dự án, chẳng hạn như -Pandroid.testInstrumentationRunnerArguments.size=small. Để đặt đối số trình chạy mặc định trong khối useJunitEngine, hãy gọi phương thức addInputProperty, chẳng hạn như addInputProperty("android-test.instrumentation-args", "size=small").
Thu thập mức độ sử dụng mã trên thiết bị
Mức độ sử dụng mã trên thiết bị yêu cầu một bản dựng có thể gỡ lỗi và một thiết bị chạy Android 9 (cấp độ API 28) trở lên. Để thu thập mức độ sử dụng cho một bộ kiểm thử đo lường hoặc kết hợp, hãy hoàn tất các bước sau:
- Đặt thuộc tính
android.experimental.androidTest.coverage.useOnDeviceAgent=truetrong tệpgradle.properties. - Đặt thuộc tính
codeCoveragethànhtruetrên bộ ứng dụng. - Nếu có nhiều thiết bị được kết nối, hãy nhắm đến một thiết bị bằng biến môi trường
ANDROID_SERIAL.
Để biết thêm thông tin về các yêu cầu đối với tác nhân, hãy xem phần Xem báo cáo mức độ sử dụng mã (bằng tác nhân trên thiết bị).
Kiểm tra báo cáo
Sau khi chạy thử nghiệm xong, AGP sẽ tạo kết quả đầu ra theo các đường dẫn sau:
- Báo cáo thử nghiệm HTML:
{module}/build/reports/tests/{taskName}/index.html - Kết quả XML của JUnit:
{module}/build/test-results/{taskName}/ - XML và logcat theo thiết bị:
{module}/build/intermediates/{variant}/{taskName}/results/ - Báo cáo mức độ sử dụng mã:
{module}/build/reports/coverage/{suiteName}/index.html