Bộ kiểm thử đơn vị chạy các kiểm thử JVM cục bộ trong nhóm tài nguyên hostJar riêng biệt bên ngoài thư mục src/test/ mặc định của mô-đun. Định cấu hình các bộ 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 kiểm thử JUnit 5, JUnit 4 hoặc Robolectric theo từng biến thể.
Yêu cầu
Để sử dụng bộ kiểm thử đơn vị, 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
Để 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ử đơn vị
Để thêm một bộ kiểm thử đơn vị vào mô-đun ứng dụng hoặc 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ộ bên trong khối testOptions.customSuites có các khối hostJar và useJunitEngine:
android {
testOptions {
customSuites {
create("unitSuite") {
// Exact variant names to test, such as "debug" or "demoDebug".
targetVariants += listOf("debug")
hostJar {
dependencies {
// JUnit 5 API for @org.junit.jupiter.api.Test.
implementation(
"org.junit.jupiter:junit-jupiter-api:5.10.2"
)
// Optional: JUnit 4 API for @org.junit.Test.
implementation("junit:junit:4.13.2")
}
}
useJunitEngine {
// Add "junit-jupiter" (JUnit 5), "junit-vintage" (JUnit 4),
// or both.
includeEngines += listOf("junit-jupiter", "junit-vintage")
enginesDependencies.add(
"org.junit.jupiter:junit-jupiter-engine:5.10.2"
)
enginesDependencies.add(
"org.junit.vintage:junit-vintage-engine:5.10.2"
)
enginesDependencies.add(
"org.junit.platform:junit-platform-launcher:1.10.2"
)
}
targets {
create("default")
}
}
}
}
}
Bộ kiểm thử của bạn sẽ tự động truy cập vào các lớp và phần phụ thuộc của biến thể mục tiêu mà không kế thừa các phần phụ thuộc từ cấu hình testImplementation.
Tạo các bài kiểm thử
Đặt các bài kiểm thử đơn vị vào thư mục {module}/src/{suiteName}/kotlin/ hoặc java/ và tài nguyên kiểm thử Java vào thư mục {module}/src/{suiteName}/resources/. Ví dụ: tạo tệp app/src/unitSuite/kotlin/com/example/myapp/CalculatorSuiteTest.kt:
package com.example.myapp
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class CalculatorSuiteTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
Bao gồm tài nguyên Android và Robolectric
Để truy cập vào các tài nguyên, thành phần, tệp kê khai và lớp R đã hợp nhất của Android, chẳng hạn như cho các chương trình kiểm thử Robolectric, hãy đặt thuộc tính enableAndroidResources của giao diện TestSuiteHostJarSpec thành true:
android {
testOptions {
customSuites {
create("roboSuite") {
targetVariants += listOf("debug")
hostJar {
enableAndroidResources = true
dependencies {
implementation("junit:junit:4.13.2")
implementation("org.robolectric:robolectric:4.14.1")
implementation("androidx.test:core-ktx:1.6.1")
}
}
useJunitEngine {
includeEngines += listOf("junit-vintage")
enginesDependencies.add(
"org.junit.vintage:junit-vintage-engine:5.10.2"
)
enginesDependencies.add(
"org.junit.platform:junit-platform-launcher:1.10.2"
)
}
targets {
create("default")
}
}
}
}
}
Tuỳ chỉnh nhiệm vụ và phạm vi áp dụng
Bật phạm vi JaCoCo trên một bộ bằng thuộc tính codeCoverage. Để tuỳ chỉnh các tuỳ chọn Test của Gradle cho mỗi mục tiêu, hãy truyền một lambda cấu hình đến phương thức AgpTestSuiteTarget.configureTestTasks. Phương thức này cung cấp một thực thể TestTaskContext. Để đính kèm các nhiệm vụ xác minh vào nhiệm vụ check, hãy sử dụng phương thức withTestTaskProviders:
android {
testOptions {
customSuites {
named("unitSuite") {
// Generates JaCoCo HTML and XML coverage reports.
codeCoverage = true
targets.named("default") {
configureTestTasks { context ->
systemProperty("suite.variant", context.targetedVariant)
maxParallelForks = 2
}
}
withTestTaskProviders { context ->
if (!context.isUpdateTask) {
tasks.named("check") {
dependsOn(this@withTestTaskProviders)
}
}
}
}
}
}
}
Để áp dụng cài đặt nhiệm vụ Test cho mọi mục tiêu trong một bộ, hãy gọi phương thức AgpTestSuite.configureTestTasks trên bộ đó.
Chạy thử nghiệm và kiểm tra báo cáo
Chạy bộ kiểm thử từ dòng lệnh bằng tác vụ test{Variant}{SuiteName}{Target}TestSuite đã tạo:
./gradlew :app:testDebugUnitSuiteDefaultTestSuiteĐể chạy một lớp hoặc phương thức kiểm thử, hãy truyền tuỳ chọn --tests của Gradle:
./gradlew :app:testDebugUnitSuiteDefaultTestSuite \
--tests "com.example.myapp.CalculatorSuiteTest.addition_isCorrect"Sau khi chạy thử nghiệm xong, AGP sẽ tạo báo cáo 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}/ - Báo cáo mức độ phù hợp của JaCoCo:
{module}/build/reports/coverage/{suiteName}/index.html