Jacoco report for Cucumber Test

57 views
Skip to first unread message

Balesh koijam

unread,
Dec 13, 2018, 3:41:08 PM12/13/18
to JaCoCo and EclEmma Users

Seeking some suggestions on an issue that I am facing while trying to generate the jacoco code coverage report for Cucumber tests.

Below is the steps that I had followed: 

     1. placed the org.jacoco.agent-0.8.2-runtime.jar in my local machine. Created a simple java class with just one function.

     2. Added the BDD feature file and the step definition file so that Step definition file calls the function defined in the java class created in step1.

  1. Added the agent to JVM option of the cucumber runner configuration, refer the screen shot below:  

Capture.JPG

  1. Execute the BDD tests. the .exec file is generated.

  2. using the .exec file, a gradle task was added in the build.gradle to generate the report. below are the details of the dependencies wihch i added in my build.gradle file.apply plugin: 'java'

    apply plugin: 'maven'
    apply plugin: 'jacoco'


    jacoco {

    toolVersion = "0.8.2"
    reportsDir = file("$buildDir/jacocoHtml")

    }


    dependencies {
    testCompile(group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.2', classifier: 'runtime')
    }

    dependencies {

    testCompile 'info.cukes:cucumber-java:1.2.5'
    testCompile 'info.cukes:cucumber-jvm:1.2.5'
    testCompile 'info.cukes:cucumber-jvm-deps:1.0.5'
    testCompile 'info.cukes:cucumber-junit:1.2.5'
    testCompile 'info.cukes:cucumber-core:1.2.5'
    testCompile 'info.cukes:gherkin:2.12.2'
    testCompile 'net.masterthought:cucumber-reporting:3.8.0'
    testCompile 'com.github.mkolisnyk:cucumber-reports:1.2'
    testCompile 'com.jayway.restassured:rest-assured:2.7.0'
    testCompile 'com.opencsv:opencsv:4.0'
    testCompile 'org.hamcrest:hamcrest-library:1.1'


    }


Below is the definition of my gradle task to generate the jacoco report:

task generateJacocoUTReport(type: JacocoReport, group: "jacocotest") {
dependsOn allprojects.test
additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories = files(subprojects.sourceSets.main.output)
executionData file("$buildDir/bddTest.exec")
reports {
xml.enabled = false
csv.enabled = false
html.destination file("$buildDir/jacoco/html")
}



Jacoco reports is genearted in the location : Build/jacoco/html however the report looks very weird. below is how the reports looks like,


reprt.JPG



just to make sure, the exec file that was created after running my test was ok, i used the feature in intellj to generate the coverage report using the option : Analyze > show code coverage data.
the reports created using this option from the .exec file looks good. this confirms that exec file has the proper coverage details of my test howver there are some issues with the gradle task that i have written.

Could anyone in this group help me with this, i had been looking / reading for the solutions for more than a three days but could not find a proper solution that works.

thanks in advance. A help on this will be really appreciated.

regards
Balesh


Evgeny Mandrikov

unread,
Dec 14, 2018, 7:55:16 AM12/14/18
to JaCoCo and EclEmma Users
Why not execute Gradle with debug information and watch what it will print?

Most likely message will be

Writing bundle 'ProjectName' with 0 classes

Because such report is likely due to absence of classes to analyze, i.e. in

classDirectories = subprojects.sourceSets.main.output

Balesh koijam

unread,
Dec 17, 2018, 9:42:39 AM12/17/18
to JaCoCo and EclEmma Users
Thanks Mandrikov  for the reply,

as mentioned above, the exec file that was generated was used to generate the coverage report using the Intellji feature, where i could see the reports looking good. with this , i am assuming that classes should be there to analyze. please correct me if my understanding is wrong.

Evgeny Mandrikov

unread,
Dec 17, 2018, 10:47:53 AM12/17/18
to JaCoCo and EclEmma Users
class files are not stored inside exec-file,

location of class files used by IDE
can be different
from location of class files used by Gradle build,
and is actually different as shown on below screenshot,

And once again:
since you have trouble with generation of report using Gradle task, then you must debug Gradle task instead of focusing on IDE,
and one of the ways to start doing this - is to execute Gradle with more verbose log ("gradle --info" or "gradle --debug") and watch what will be printed during execution of your task.

screenshot.png

Balesh koijam

unread,
Dec 17, 2018, 11:53:47 AM12/17/18
to JaCoCo and EclEmma Users
thanks for the explaination.

Debuging does not give me much details, however i had updated the task script by adding some println as below:

additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
println additionalSourceDirs.size()
println additionalSourceDirs.empty

sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
println sourceDirectories.size()
println sourceDirectories.empty

classDirectories = files(subprojects.sourceSets.main.output)
println classDirectories.empty
println classDirectories.files

def execfile= executionData file("$buildDir/jacoco/bddTest.exec")
println execfile
println "$buildDir/jacoco/bddTest.exec"


below is the output:

0
true
0
true
true
[]
null
C:\Users\U645789\cucumberJacocoPoc\build/jacoco/bddTest.exec

below is how my project looks like

proj.JPG




Looks like the files and the folder locations are not specific properly. please advice.


below is the details from console after running the task with -- debug.


:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:generateJacocoUTReport UP-TO-DATE

BUILD SUCCESSFUL in 1s
5 actionable tasks: 5 up-to-date
11:52:41 AM: Task execution finished 'generateJacocoUTReport'.

thanks

Balesh koijam

unread,
Dec 17, 2018, 12:10:42 PM12/17/18
to JaCoCo and EclEmma Users
found the issue. it was with the path of the dirs and the file which i mentioned in my task script.
i can see the reports with proper coverage.

thanks mandrikov for the help.

now i had updated to the below :

additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)

sourceDirectories = files(sourceSets.main.allSource.srcDirs)
classDirectories = files(sourceSets.main.output)




Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages