Gradle: how to create a coverage report for an integration test using jacoco

I am new to gradle. I am using the code below. But it generates coverage for unit test cases. But this did not create integration for test cases. I have test classes in src / test / java package.

test { dependsOn jettyRunWar ignoreFailures true finalizedBy jettyStop } apply plugin: 'jacoco' jacocoTestReport { group = "Reporting" description = "Generate Jacoco coverage reports after running tests." additionalSourceDirs = files(sourceSets.main.allJava.srcDirs) } 
+11
source share
2 answers

Using Gradle 5.4.1 (and now 5.5.1), I was able to get a report after any test task, currently I have test and integrationTest tasks.

EDIT2 : the solution is the same, I just tweaked

  • assigning reports to use jacoco.reportsDir ,
  • now tasks.withType(Test) used for executeData, and not just [test, integrationTest]
  • executionData is configured in the doFirst block instead of doLast

EDIT : After viewing the JacocoReport documentation, there is a variant of JacocoReport: executeData that directly accepts Gradle tasks. This works because the JaCoCo plugin adds the JacocoTaskExtension extension to all tasks like Test . Which is then less error prone. The task of the report is:

 jacocoTestReport { doFirst { // The JaCoCo plugin adds a JacocoTaskExtension extension to all tasks of type Test. // Use task state to include or not task execution data // https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskState.html executionData(tasks.withType(Test).findAll { it.state.executed }) } reports { xml.enabled true xml.destination(file("${jacoco.reportsDir}/all-tests/jacocoAllTestReport.xml")) html.enabled true html.destination(file("${jacoco.reportsDir}/all-tests/html")) } } 

And the same trick can be applied to the sonarqube task:

 sonarqube { group = "verification" properties { // https://jira.sonarsource.com/browse/MMF-1651 property "sonar.coverage.jacoco.xmlReportPaths", jacocoTestReport.reports.xml.destination properties["sonar.junit.reportPaths"] += integrationTest.reports.junitXml.destination properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs // ... other properties } } 

Old but very working answer. Also, using the above information (that the Test task has been extended by JacocoTaskExtension ), you can replace the manual configuration of file executionData with test.jacoco.destinationFile and integrationTest.jacoco.destinationFile .

 // Without it, the only data is the binary data, // but I need the XML and HTML report after any test task tasks.withType(Test) { finalizedBy jacocoTestReport } // Configure the report to look for executionData generated during the test and integrationTest task jacocoTestReport { executionData(file("${project.buildDir}/jacoco/test.exec"), file("${project.buildDir}/jacoco/integrationTest.exec")) reports { // for sonarqube xml.enabled true xml.destination(file("${project.buildDir}/reports/jacoco/all-tests/jacocoAllTestReport.xml")) // for devs html.enabled true html.destination file("${project.buildDir}/reports/jacoco/all-tests/html") } } sonarqube { group = "verification" properties { // https://jira.sonarsource.com/browse/MMF-1651 property "sonar.coverage.jacoco.xmlReportPaths", ${project.buildDir}/test-results/integrationTest" properties["sonar.junit.reportPaths"] += "${project.buildDir}/test-results/integrationTest" properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs // ... other properties } } project.tasks["sonarqube"].dependsOn "jacocoTestReport" 
+4
source

It sounds like you need to say that build.gradle is where your Intergration test tests (i.e. the folder containing these IT tests) using sourceSets. In my case, I have a source in src / java (instead of src / main / java - gradle default) .. my unit tests (Junit) in the test / java folder and my integration tests in the src / java-test folder.

 sourceSets { main { java { srcDir 'src/java' } } test { java { srcDir 'test/java' } resources { srcDir 'test/resources' srcDir 'conf' } } integrationTest { java { srcDir 'src/java-test' } } } 

Then I have an IntegrationTest task like ... which you can configure, since you may not have cleanTest (the custom task I created), so you can ignore it depends on ... I think in your case you will use something like jettyStart, because you use it for IT tests (starting a container to run IT tests, and then a finalized option to stop the berth .. berth plugin)

 task integrationTest( type: Test, dependsOn: cleanTest ) { jacoco { //destinationFile = file("$buildDir/jacoco/jacocoTest.exec") destinationFile = file("$buildDir/jacoco/integrationTest.exec") //classDumpFile = file("$buildDir/jacoco/classpathdumps") classDumpFile = file("$buildDir/classes/integrationTest") } testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath } 

SEE this post for a more detailed output structure and script that I have at my end. I get .exec for both unit tests (test.exec) and IT tests intergrationTest.exec .. but I do not get jacoco.xml / jacocoHtml reports for both tests. I also found that if I ran "gradle clean build" (including calling the "test" task) and "gradle clean build integrationTest", then it overwrites the unit test data in the build / test-results and assembly / reports / tests folder.

Jacobi and Integration Tests - Individual and General

NOTE In my case, jacocoTestReport is defined in the global gradle init.d folder in one of the normal gradle files. This will help us not to include the same code in all / at the project level build.gradle.

+3
source

Source: https://habr.com/ru/post/1257997/


All Articles