1

Our project is using Gradle 3.5, jacoco 0.8.1

It has 3 modules -- module-A, module-B and module-C, and its code coverage is 50%, 6% and 42% separately, and the code coverage for the whole project is 38%.

Now we want to use the current code coverage for the whole project as the threshold, that means if the code coverage of the whole project is less than 38%, the build will fail.

I tried the solution in Minimum code coverage threshold in Jacoco Gradle

jacocoTestCoverageVerification {
violationRules {
    rule {
        limit {
            minimum = 0.38
        }
    }
}

but failed, it hints that module-B violates the rules, instruction covered ratio is 0.06, but expect is 0.38, seems it only suits for the module level, not the whole project level.

Also I tried to use element = 'GROUP', but seems no effect. (https://www.eclemma.org/jacoco/trunk/doc/api/org/jacoco/core/analysis/ICoverageNode.ElementType.html)

Anyone knows how to set the minimum code coverage threshold for the whole project and not for the module level?

thanks,

zeroxin
  • 19
  • 4

2 Answers2

0

I suggest having a separate project (module) within your multi-module build for reporting on the whole project. You might need the JacocoMerge task too. Let's assume a, b and c are java projects. Eg

Eg:

def javaProjects = [':a', ':b', ':c']
javaProjects.each {
   project(it) {
      apply plugin: 'java' 
      apply plugin: 'jacoco' 
   } 
} 
project(':report') {
    FileCollection execData = files(javaProjects.collect { project(it).tasks.withType(Test).jacoco.destinationFile }) 
    FileCollection sourceDirs = files(javaProjects.collect { project(it).sourceSets.main.java.srcDirs }) 
    FileCollection classDirs = files(javaProjects.collect { project(it).sourceSets.main.java.output.classesDirs }) 
    def testTasks = javaProjects.collect { project(it).tasks.withType(Test)} 


   task jacocoMerge(type: JacocoMerge) {
      dependsOn testTasks
      executionData execData
      jacocoClasspath = classDirs
   } 
   task coverageVerification(type: JacocoCoverageVerification) {
      dependsOn jacocoMerge
      executionData jacocoMerge.destinationFile
      sourceDirectories srcDirs
      classDirectories classDirs
      violationRules.rule.limit.minimum = 0.38
   } 
   task jacocoReport(type: JacocoReport) {
      dependsOn jacocoMerge
      executionData jacocoMerge.destinationFile
      sourceDirectories srcDirs
      classDirectories classDirs
   }
} 
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Hi, Iance, thanks for your reply, but our project exists more than one year, if move report to a separate project, it will need many works to do, so far it is impracticable. – zeroxin Jul 17 '18 at 09:47
  • Well... this would also work in one of the java projects. Can't see how this would need "many works" to separate into its own project. Surely its just a few lines of Gradle config? And maybe some config in CI? – lance-java Jul 17 '18 at 09:53
  • Hi, Iance, because we use Sprint to dev(two weeks as a Sprint) and also if move those to a separate project, we need to create a new project and put it to git repository, modify the jacoco code coverage report, modify CICD jenkins job, gradlew configuration, and also some integration tests and e2e tests, so it is not an easy work in short time. – zeroxin Jul 18 '18 at 02:16
  • I meant a separate project (module) within the same multi-module build as you can see from my sample gradle script. So same git repository, same build – lance-java Jul 18 '18 at 09:03
  • Yes, I find another solution for it, that is to write limit rule in each module, such as for module-A, its code coverage is 50%, so in the build.gradle file in module-A, the rule is as following: jacocoTestCoverageVerification { violationRules { rule { limit { minimum = 0.5 } } } – zeroxin Jul 19 '18 at 10:09
  • If you have tests in one module which cover code from another I prefer to do coverage and verification once for the entire multi-module build. – lance-java Jul 19 '18 at 11:25
0

Yes, I find another solution for it, that is to write limit rule in each module, such as for module-A, its code coverage is 50%, so in the build.gradle file in module-A, the rule is as following:

// for moudle-A module
jacocoTestCoverageVerification {
violationRules {
    rule {
        limit {
            minimum = 0.5
        }
    }
}
zeroxin
  • 19
  • 4
  • Yep, good idea! You can put it under `subprojects {}` section to apply for all your modules – Max Oct 25 '19 at 08:33