【问题标题】:Gradle Merge Junit ReportGradle 合并 Junit 报告
【发布时间】:2016-12-22 11:41:55
【问题描述】:

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
}

【问题讨论】:

    标签: gradle junit


    【解决方案1】:

    您应该能够使用 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 依赖项。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 2018-11-21
        • 2023-03-05
        • 1970-01-01
        • 2017-08-26
        相关资源
        最近更新 更多