Gradle Merge Junit报告

时间:2016-08-16 07:37:56

标签: gradle junit

在gradle中是否可以将junit xml报告合并到单个测试报告文件中。

当我们在ant中使用cp-suite执行IntegrationTestSuite.java时,只有一个junit报告。在gradle上创建多个junit报告。

IntegrationTestSuite.java

@RunWith(Categories.class)
@Categories.IncludeCategory(IntegrationTests.class)
@Categories.ExcludeCategory(DebugJenkinsTests.class)
@Suite.SuiteClasses(AllTestSuite.class)
public class IntegrationTestSuite { /* nop */
}

build.xml

剪断
<junit>
   <formatter type="xml" />
      <batchtest todir="${testreport.dir}">
          <fileset dir="${test-src.dir}">
              <include name="**/IntegrationTestSuite.java" />
          </fileset>
       </batchtest>
</junit>

build.gradle

剪断
task integrationTestSandro(type: Test) {
    reports.html.enabled = false
    include '**/IntegrationTestSuite*'
    reports.junitXml.destination = "$buildDir/test-results/integration"
    maxHeapSize = testTaskMaxHeapSize
    jvmArgs testTaskJVMArgs
}

2 个答案:

答案 0 :(得分:4)

您应该能够使用Ant任务JUnitReport来实现目标。以下内容应该有效:

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')
        }
    }
}

请记住,您必须声明存储库以允许Gradle解析ant-junit依赖项。

答案 1 :(得分:0)

想添加到本杰明的答案中,因为我必须进行一些更改才能使其正常工作。这就是我的文件最终的样子。

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) 
                }
            } 
        } 
    } 
} 

希望这对遇到此问题的人有所帮助。