Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Merge Junit Report

Tags:

junit

gradle

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
}
like image 753
sandrozbinden Avatar asked Aug 16 '16 07:08

sandrozbinden


2 Answers

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.

like image 98
Benjamin Muschko Avatar answered Dec 06 '22 03:12

Benjamin Muschko


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.

like image 33
Ganesh Datta Avatar answered Dec 06 '22 04:12

Ganesh Datta