[英]Is there an easy way to exclude classes from test coverage using Java Annotations in Spring Boot?
I have a (gradle + kotlin) spring boot project with several DTOs, configuration classes, constants etc. that I don't want to be analyzed during test coverage analysis. 我有一个(gradle + kotlin)春季启动项目,其中包含几个DTO,配置类,常量等,我不想在测试覆盖率分析期间进行分析。
Is there a convenient Java notation that I can use? 我可以使用一种方便的Java表示法吗?
You said that you are using kotlin and gradle. 您说您正在使用Kotlin和Gradle。 So I assume that you are using jacoco for test coverage. 因此,我假设您正在使用jacoco进行测试。
This is one of the jacoco coverage excludes example. 这是jacoco涵盖范围之一,不包括示例。
jacocoTestReport {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['**/*Application**'])
})
}
}
This is what ended up working for me. 这就是最终为我工作的原因。 Had to write some custom custom filtering logic in a very hacky sort of way, but it did the job. 不得不以一种非常古怪的方式编写一些自定义的自定义过滤逻辑,但是它确实可以做到。
Upvoting @Min Hyoung Hong's answer for leading me down the right track. 支持@Min Hyoung Hong的答案,将我引向正确的方向。
build.gradle.kts build.gradle.kts
tasks {
withType<KotlinCompile<KotlinJvmOptions>> {
kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
kotlinOptions.jvmTarget = "1.8"
}
withType<JacocoReport> {
reports {
xml.isEnabled = false
csv.isEnabled = false
html.destination = file("$buildDir/jacocoHtml")
}
afterEvaluate {
val filesToAvoidForCoverage = listOf(
"/dto",
"/config",
"MyApplicationKt.class"
)
val filesToCover = mutableListOf<String>()
File("build/classes/kotlin/main/app/example/core/")
.walkTopDown()
.mapNotNull { file ->
var match = false
filesToAvoidForCoverage.forEach {
if (file.absolutePath.contains(it)) {
match = true
}
}
return@mapNotNull if (!match) {
file.absolutePath
} else {
null
}
}
.filter { it.contains(".class") }
.toCollection(filesToCover)
classDirectories = files(filesToCover)
}
}
}
我不KHOW你用什么样的工具来覆盖你的测试类,但在这里是如何从测试覆盖率排除类与jacoco一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.