汇总gradle多模块测试结果和汇总Gradle多模块代码覆盖率jacoco报告
1、Gradle如果测试失败,如何继续构建
默认情况下,如果任何单元测试失败,则Gradle构建过程将停止并失败。 即使测试过程失败,也可以继续构建过程的几种方法。
1.1、忽略测试失败
build.gradle
test {
ignoreFailures = true
}
1.2、排除失败的测试
test {
exclude '**/ThisIsFailedTestExample.class'
exclude '**/*FailedTestExample*'
}
1.3、跳过了整个测试过程
gradle build -x test
2、汇总gradle多模块测试结果
默认java插件的test指令生成的测试报告保存在各个子模块build目录的位置
设置不生成测试结果报告
// Disable the test report for the individual test task
test {
reports.html.enabled = false
reports.junitXml.enabled = false
}
汇总 Gradle 多模块测试结果,结果保存在 项目根目录\build\reports\allTests
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
// Disable the test report for the individual test task
test {
reports.html.enabled = false
reports.junitXml.enabled = false
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
}
// 汇总 Gradle 多模块测试结果
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test {
ignoreFailures = true // 忽略测试失败
}
}
testReport指令集成
执行testReport指令,结果保存在项目根目录\build\reports\allTests
3、汇总 Gradle 多模块代码覆盖率报告
引入 jacoco 插件后,在verification就会集成 jacocoTestReport 指令
执行jacocoTestReport 指令会在各个子模块build目录下生成该子模块的代码覆盖率报告
汇总 Gradle 多模块代码覆盖率报告,结果保存在 项目根目录\build\reports\jacoco\jacocoRootReport\html
plugins {
id 'java'
id 'jacoco'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
}
// 汇总 Gradle 多模块代码覆盖率报告
task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
dependsOn(subprojects.test)
additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(subprojects.sourceSets.main.output)
executionData.from = files(subprojects.jacocoTestReport.executionData)
reports {
html.enabled true
}
}
参考案例
根目录的build.gradle
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
plugins { id 'java' id 'jacoco' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() } subprojects { apply plugin: 'java' apply plugin: 'jacoco' sourceCompatibility = 1.8 // Disable the test report for the individual test task test { reports.html.enabled = false reports.junitXml.enabled = false } repositories { mavenCentral() } dependencies { testImplementation 'junit:junit:4.13.2' } } // 汇总 Gradle 多模块代码覆盖率报告 task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') { description = 'Generates an aggregate report from all subprojects' dependsOn(subprojects.test) additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs) sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs) classDirectories.from = files(subprojects.sourceSets.main.output) executionData.from = files(subprojects.jacocoTestReport.executionData) reports { html.enabled true } } // 汇总 Gradle 多模块测试结果 task testRootReport(type: TestReport) { destinationDir = file("$buildDir/reports/allTests") // Include the results from the `test` task in all subprojects reportOn subprojects*.test { ignoreFailures = true // 忽略测试失败 } }
如果某个子模块不生成 jacoco/test.exec会报错,参考下面配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
buildscript {
repositories {
mavenLocal()
}
dependencies {
//spring-boot插件
classpath("org.springframework.boot:org.springframework.boot.gradle.plugin:2.6.3")
}
}
plugins {
id 'java'
id 'jacoco'
}
apply from: "./config.gradle"
group = 'xxx'
version = '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
maven {
url "http://xxx/nexus/content/groups/public/"
}
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-web:2.6.3')
}
tasks.withType(Jar) {
destinationDir = file("../build/libs") //$rootDir 取项目的顶级目录
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
sourceCompatibility = 1.8
repositories {
maven {
url "http://xxx/nexus/content/groups/public/"
}
}
dependencies {
testImplementation 'junit:junit:4.13.1'
}
}
def printProjectName = {
project ->
println '需要进行jacoco聚合的子工程: ' + project.name
}
/*
// 聚合各个子模块的build/jacoco/test.exec
// 构建jacoco代码覆盖率的步骤
gradle clean
gradle test --continue // 执行test用例,--continue 用例报错继续执行
//gradle jacocoTestReport // 在子模块下面生成该子模块的单测覆盖率
gradle jacocoRootReport // 聚合各个子模块,生成整个工程的单测覆盖率
// 一步生成jacoco代码覆盖率的指令
gradle clean test --continue jacocoRootReport
*/
task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
//dependsOn(subprojects.test)
// 需要生成test.exec的子模块(如果不排除不生成单测的模块,执行gradle jacocoRootReport会报找不到该模块的test.exec)
def projects = ['xxx', 'yyy']
def jacocoProjects = subprojects.findAll{ projects.contains(it.getName()) }
doFirst {
jacocoProjects.forEach(printProjectName)
}
additionalSourceDirs.from = files(jacocoProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(jacocoProjects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(jacocoProjects.sourceSets.main.output)
executionData.from = files(jacocoProjects.jacocoTestReport.executionData)
reports {
html.enabled true
}
}
// 忽略测试失败
test {
ignoreFailures = true
}
// 汇总 Gradle 多模块测试结果
task testRootReport(type: TestReport) {
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test {
ignoreFailures = true // 忽略测试失败
}
}
// 一步生成测试结果和代码覆盖率报告
// gradle clean test --continue jacocoRootReport testRootReport
4、利用 jacococli 生成覆盖率
利用 jacococli 生成覆盖率:仅供参考,根据前面的配置不仅可以生成单个模块的覆盖率,而且可以聚合生成整体项目的覆盖率。
当已经生成jacoco/test.exec,结合源代码和编译classes生成jacoco覆盖率报告
java -jar jacococli.jar report ./test.exec --classfiles ../classes --sourcefiles ../../src/main/java --encoding utf-8 --html jacoReport
posted on 2022-04-04 13:52 wenbin_ouyang 阅读(1882) 评论(0) 编辑 收藏 举报