文章目录
前言
如题。费了一番功夫才大概搞明白gradle项目中如何使用jacoco插件,这里简单记录一下。
添加jacoco插件
具体的gradle项目内容,以及测试用例内容就不介绍了。意义不大。
这里主要介绍build.gradle
的内容:
plugins {
id 'java'
id 'jacoco'
}
//apply plugin: 'java'
//apply plugin: 'jacoco'
repositories {
mavenLocal()
mavenCentral()
maven { url '/home/apr/env/mavenDownload/' }
// Use JCenter for resolving dependencies.
jcenter()
}
version = '0.0.1'
group = 'Python_Patch_Parser'
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
}
jacocoTestReport {
reports {
// xml is usually used to integrate code coverage with
// other tools like SonarQube, Coveralls or Codecov
xml.enabled false
// HTML reports can be used to see code coverage
// without any external tools
html.enabled true
}
}
// dependsOn方法用来声明一个任务依赖于一个或者多个任务
// Gradle并不保证依赖的任务能够按顺序执行,dependsOn方法只是定义这些任务应该在这个任务之前执行,但是这 些依赖的任务具体怎么执行它并不关心,如果你习惯用命令式的构建工具来定义依赖(比如ant)这可能会难以理 解。在Gradle里面,执行顺序是由任务的输入输出特性决定的,这样做有很多优点,比如你想修改构建逻辑的时 候你不需要去了解整个任务依赖链,另一方面,因为任务不是顺序执行的,就可以并发的执行来提高性能。
check.dependsOn jacocoTestReport
运行./gradlew build
就可以看到:
build/reports/jacoco/test/html/index.html
文件:
build/reports/tests/test/index.html
文件: