Search code examples
gradlejunit

Gradle Merge Junit Report


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
}

Solution

  • 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.