Is there a way in gradle that the junit xml report can be merged into a single test report file.
When we executed our IntegrationTestSuite.java with cp-suite in ant there is a single junit report. On gradle multiple junit reports are created.
IntegrationTestSuite.java
@RunWith(Categories.class)
@Categories.IncludeCategory(IntegrationTests.class)
@Categories.ExcludeCategory(DebugJenkinsTests.class)
@Suite.SuiteClasses(AllTestSuite.class)
public class IntegrationTestSuite { /* nop */
}
Snipped from build.xml
<junit>
<formatter type="xml" />
<batchtest todir="${testreport.dir}">
<fileset dir="${test-src.dir}">
<include name="**/IntegrationTestSuite.java" />
</fileset>
</batchtest>
</junit>
Snipped from build.gradle
task integrationTestSandro(type: Test) {
reports.html.enabled = false
include '**/IntegrationTestSuite*'
reports.junitXml.destination = "$buildDir/test-results/integration"
maxHeapSize = testTaskMaxHeapSize
jvmArgs testTaskJVMArgs
}
You should be able to use the Ant task JUnitReport to achieve your goal. Something like the following should work:
configurations {
antJUnit
}
dependencies {
antJUnit 'org.apache.ant:ant-junit:1.9.7'
}
task mergeJUnitReports {
ext {
resultsDir = file("$buildDir/allreports")
targetDir = file("$buildDir/test-results/merged")
}
doLast {
ant.taskdef(name: 'junitreport',
classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator',
classpath: configurations.antJUnit.asPath)
ant.junitreport(todir: resultsDir) {
fileset(dir: resultsDir, includes: 'TEST-*.xml')
report(todir: targetDir, format: 'frames')
}
}
}
Keep in mind that you will have to declare a repository to allow Gradle to resolve the ant-junit
dependency.
Wanted to add onto Benjamin's answer, since I had to make some changes to get it to work. Here's what my file ended up looking like.
configurations {
antJUnit
}
dependencies {
antJUnit 'org.apache.ant:ant-junit:1.9.7'
}
subprojects {
apply plugin: 'java'
// Disable the test report for the individual test task
test {
reports.html.enabled = false
reports.junitXml.enabled = true
}
}
// Compile all the test results into a single one.
task testReport {
ant.taskdef(name: 'junitreport', classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator', classpath: configurations.antJUnit.asPath)
dependsOn subprojects*.test
doFirst {
mkdir "$buildDir/test-results"
ant.junitreport(todir: "$buildDir/test-results") {
subprojects.each {
if (it.testResultsDir.exists()) {
fileset(dir: it.testResultsDir)
}
}
}
}
}
Hope this helps someone who comes across this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With