Jacoco Unit and Integration Tests coverage - individual and overall [closed]

落花浮王杯 提交于 2019-11-28 11:29:20

Found answer to my 2nd question. High level info:

  1. Gradle 1.6 jacocoTestReport uses different variables, Gradle >=1.7 uses different.

    For ex: we can tweak Unit tests and Integration Tests .exec file creation by changing "test" or "integrationTest" task by using the CORRECT variables -or it wont work n generate "test.exec" and "integrationTest.exec" default file names. See example below.

task integrationTest(type: Test) OR test { ... } section can have the correct variables for the given Gradle version that we are using.

task integrationTest (type: Test) { 
testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

   testReportDir = file("$buildDir/reports/tests/IT")
   testResultsDir = file("$buildDir/test-results/IT")



  ignoreFailures = true
   jacoco {

 //This works with 1.6
 destPath = file("$buildDir/jacoco/IT/jacocoIT.exec")
 classDumpPath = file("$buildDir/jacoco/IT/classpathdumps")

 /*
 Following works only with versions >= 1.7 version of Gradle
 destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
 classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
 */
   }
}

Similarly, for test { .... } task, you can define it as ../../UT/jacocoUT.exec and ../../UT/classpathdumps...

  1. .sonar folder gets created if I run "sonar-runner" Linux/Unix sonar-runner executable in my project's workspace, BUT if I run Jenkins job which calls Gradle "clean build integrationTest jacocoTestReport sonarRunner", then build/sonar folder is created and becomes the WORKING DIR for SONAR (this shows up during the output).

  2. In Jenkins > under Post build section, I mentioned the following and NOW, jacoco code coverage report on Jenkins Dashboard for both jacoco files (.html and .xml) - includes Unit and Integration tests code coverage data.

Record Jacoco coverage report section in Jenkins has the following boxes:

I mentioned:

Path to exec files: /build/jacoco/UT/jacocoUT.exec, */build/jacoco/IT/jacocoIT.exec Path to class dirs: */build/jacoco//classpathdumps/com/thc (this is the location where Jacoco instrumented classes sit).. both build/jacoco/UT/classpathdumps/com/thc and build/jacoco/IT/classpathdumps/com/thc will be picked as ** will be replaced for any folder under build/jacoco. this value can be set to "build/classes" folder as well.

Path to source files: ** (if I use src/java, few of the links for source file don't work i.e. file contents don't show up).. used ** .. it works now.

rest boxes - left blank.

  1. Still wondering - why overall-jacoco.exec is not having the file size = sum of both jacocoUT.exec and jacocoIT.exec

At this point, I'm able to see Jacoco code coverage for both Unit and Integration Tests i.e. via visiting the Jacoco code coverage image on job's dashboard and visiting source code links and also if you go and browse "build/reports/jacoco/html/index.html" or "build/jacocoHtml/index.html" file.

Still trying to find out - what needs to be done for SONAR to pick these 2 .exec files (I have valid values set for sonar.xxx variurs variables for sources, tests, binaries, ...reportsPath etc for UT / IT exec files... on SonarQube dashboard, Unit test coverage is showing up fine but Integration tests coverage is still 0.0%.

I'll paste the copy of both my common.gradle and project's build.gradle file soon .... to have a better look.

OK, found the solution to UT/IT folder issue and my question (1) in this post. It's actually the "cleanTest" task which is one of the default RULE in Gradle.

Running "gradle tasks -all" gives a big output of tasks that Gradle supports and this output at the end tells about such Rules where if we call clean, then it'll wipe those folders. As you see in my code above, integrationTest was dependent upon cleanTest, thus when I called "gradle clean build integrationTest" it ran units tests first (via build task as Unit tests runs by default with the build step in Gradle) and then integration tests via "integrationTest" task, therefore, while running integration tests, it called cleanTest task, which wiped out "UT" folder which i have mentioned in a common gradle script (/init.d/commmon-some-name.gradle file) like I mentioned IT folders for reports/results directories.

Removing cleanTest as a dependsOn from integrationTest task, resolved wiping out issue.

task integrationTest( type: Test, dependsOn: cleanTest ) {
//task integrationTest( type: Test ) {



Output of following command: showing only last few lines...

gradle tasks -all

integrationTest
    classes - Assembles binary 'main'.
    cleanTest
    compileIntegrationTestJava - Compiles source set 'integrationTest:java'.
    compileJava - Compiles source set 'main:java'.
    integrationTestClasses - Assembles binary 'integrationTest'.
    processIntegrationTestResources - Processes source set 'integrationTest:resources'.
    processResources - Processes source set 'main:resources'.
jarService
sonarRunner [test]

Rules
-----
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
Pattern: clean<TaskName>: Cleans the output files of a task.

BUILD SUCCESSFUL
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!