我的项目结构如下,
--项目
--Dao
--服务
--控制器
--测试
--build.gradle.kts
我只在控制器中进行集成测试。我的所有子模块(DAO、服务和控制器)都是 gradle 项目,其中包含 build.gradle.kts。
以下是我的父模块中的 build.gradle.kts,即项目内部
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val Project.`java`: JavaPluginConvention
get() = convention.getPluginByName("java")
plugins {
val kotlinVersion = "1.3.61"
val testLoggerVersion = "1.6.0"
val dokkaVersion = "0.9.18"
base
jacoco
kotlin("jvm") version kotlinVersion apply false
maven
id("com.adarshr.test-logger") version testLoggerVersion apply false
id("org.jetbrains.dokka") version dokkaVersion apply false
}
jacoco {
toolVersion = jacocoVersion
reportsDir = file("$buildDir/reports/jacoco")
}
allprojects {
version = "dev"
repositories {
jcenter()
mavenCentral()
}
}
subprojects {
apply {
plugin("kotlin")
plugin("jacoco")
plugin("com.adarshr.test-logger")
plugin("org.jetbrains.dokka")
}
dependencies {
"implementation"(kotlin("stdlib"))
"implementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")
"implementation"("com.fasterxml.jackson.module:jackson-module-kotlin:$fasterxmlVersion")
"implementation"("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$fasterxmlVersion")
"implementation"("org.koin:koin-core:$koinVersion")
"testImplementation"("org.koin:koin-test:$koinVersion")
"testImplementation"("io.mockk:mockk:$mockkVersion")
"testImplementation"("org.junit.jupiter:junit-jupiter-api:$jUnitVersion")
"testImplementation"("org.junit.jupiter:junit-jupiter-params:$jUnitVersion")
"testImplementation"("org.testcontainers:junit-jupiter:$testcontainersVersion")
"testImplementation"("org.testcontainers:testcontainers:$testcontainersVersion")
"testImplementation"("org.testcontainers:postgresql:$testcontainersVersion")
"testRuntime"("org.junit.jupiter:junit-jupiter-engine:$jUnitVersion")
}
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
from(java.sourceSets["main"].output)
dependsOn(configurations["runtimeClasspath"])
from({
configurations["runtimeClasspath"]
.filter { it.name.endsWith("jar") }
.map { zipTree(it) }
})
}
tasks.register<Zip>("uberZip") {
from(java.sourceSets["main"].output)
dependsOn(configurations["runtimeClasspath"])
from({
configurations["runtimeClasspath"]
.filter { it.name.endsWith("jar") }
.map { zipTree(it) }
})
}
tasks.withType<DokkaTask> {
outputFormat = "html"
outputDirectory = "$buildDir/javadoc"
}
tasks.withType<KotlinCompile> {
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
kotlinOptions {
jvmTarget = javaVersion
apiVersion = kotlinVersion
languageVersion = kotlinVersion
}
}
tasks.withType<JacocoReport> {
reports {
html.apply {
isEnabled = true
destination = file("$buildDir/reports/jacoco")
}
csv.isEnabled = false
}
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.map {
fileTree(it).apply {
exclude("io/company/common/aws/test/support/**")
exclude("io/company/common/system/**")
}
}))
}
}
tasks.withType<Test> {
useJUnitPlatform()
testLogging.showStandardStreams = true
testLogging {
events("PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 jacoco 代码覆盖率。当我运行时,./gradlew clean test jacocoTestReport我只获得控制器的覆盖范围,而不是服务和 daos 的覆盖范围。
我的gradle版本是5.4.1。
如何获得涵盖所有测试模块的测试覆盖率综合报告?我尝试了 stackoverflow 上的多个链接,但没有成功。
您可以为此使用JacocoMerge任务,该任务是为该用例显式创建的。它将获取子项目的单独报告并将它们合并为一份报告。将以下代码片段添加到您的根项目中并根据需要进行调整。
val jacocoMerge by tasks.registering(JacocoMerge::class) {
subprojects {
executionData(tasks.withType<JacocoReport>().map { it.executionData })
}
destinationFile = file("$buildDir/jacoco")
}
tasks.register<JacocoReport>("jacocoRootReport") {
dependsOn(jacocoMerge)
sourceDirectories.from(files(subprojects.map {
it.the<SourceSetContainer>()["main"].allSource.srcDirs
}))
classDirectories.from(files(subprojects.map { it.the<SourceSetContainer>()["main"].output }))
executionData(jacocoMerge.get().destinationFile)
reports { // <- adjust
html.isEnabled = true
xml.isEnabled = true
csv.isEnabled = false
}
}
Run Code Online (Sandbox Code Playgroud)
现在 agradle jacocoRootReport将为您提供整体覆盖范围。
| 归档时间: |
|
| 查看次数: |
2481 次 |
| 最近记录: |