Appearance
测试报告
默认报告位置
Gradle 运行测试后自动生成报告:
build/reports/tests/test/
├── index.html ← 总览页面(在浏览器中打开)
├── classes/ ← 各测试类的详细页面
├── packages/ ← 按包分组
└── css/ js/ ← 报告样式资源bash
# 运行测试后打开报告
./gradlew test
# Linux/macOS
open build/reports/tests/test/index.html
# Windows
start build\reports\tests\test\index.htmlJUnit XML 报告(CI 使用)
JUnit XML 格式报告位于 build/test-results/,可被 Jenkins、GitLab CI 等解析:
build/test-results/test/
└── TEST-com.example.UserServiceTest.xml聚合报告(多项目)
kotlin
// 根项目 build.gradle.kts
plugins {
`test-report-aggregation`
}
dependencies {
testReportAggregation(project(":core"))
testReportAggregation(project(":web"))
testReportAggregation(project(":service"))
}
// 生成聚合报告
// ./gradlew testAggregateTestReport自定义报告位置
kotlin
tasks.named<Test>("test") {
reports {
html.required.set(true)
html.outputLocation.set(layout.buildDirectory.dir("test-reports/html"))
junitXml.required.set(true)
junitXml.outputLocation.set(layout.buildDirectory.dir("test-reports/xml"))
}
}JaCoCo 覆盖率报告
kotlin
plugins {
java
jacoco
}
tasks.named<JacocoReport>("jacocoTestReport") {
dependsOn(tasks.named("test"))
reports {
xml.required.set(true) // 用于 SonarQube 等工具
html.required.set(true) // 人工查看
csv.required.set(false)
}
}
// 覆盖率检查(低于阈值时构建失败)
tasks.named<JacocoCoverageVerification>("jacocoTestCoverageVerification") {
violationRules {
rule {
limit {
minimum = "0.80".toBigDecimal() // 至少 80% 覆盖率
}
}
}
}
// check 任务包含覆盖率检查
tasks.named("check") {
dependsOn(tasks.named("jacocoTestCoverageVerification"))
}bash
# 生成覆盖率报告
./gradlew test jacocoTestReport
# 查看报告
# open build/reports/jacoco/test/html/index.html下一步
- 打包 JAR - 构建 JAR 文件
- JaCoCo 覆盖率 - 代码覆盖率详细配置