Ответ 1
Используя Gradle 5.4.1 (а теперь и 5.5.1), я смог получить отчет после любого тестового задания, в настоящее время у меня есть задачи test
и integrationTest
.
РЕДАКТИРОВАТЬ2: решение то же самое, я просто подправил
- назначение отчетов для использования
jacoco.reportsDir
, - теперь для executeData используется
tasks.withType(Test)
, а не просто[test, integrationTest]
- настройка
executionData
выполняется в блокеdoFirst
вместоdoLast
РЕДАКТИРОВАТЬ: После просмотра документации JacocoReport
, есть вариант JacocoReport: executeData, который непосредственно принимает задачи Gradle. Это работает, потому что плагин JaCoCo добавляет расширение JacocoTaskExtension
ко всем задачам типа Test
. Который тогда меньше подвержен ошибкам. Задачей отчета становится:
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"))
}
}
И тот же прием можно применить к задаче sonarqube
:
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
}
}
Старый, но очень рабочий ответ. Также, используя приведенные выше сведения (что задача Test
расширена JacocoTaskExtension
), можно заменить ручную настройку file
executionData
на test.jacoco.destinationFile
и 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"