com.ullink.gradle.nunit.NUnitTestResultsMerger.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-nunit-plugin Show documentation
Show all versions of gradle-nunit-plugin Show documentation
gradle-nunit-plugin is a Gradle plugin that enables NUnit testing
package com.ullink.gradle.nunit
import groovy.xml.XmlUtil
import java.nio.charset.StandardCharsets
class NUnitTestResultsMerger {
void merge(List files, File outputFile) {
outputFile.write(merge(files.collect { it.text }), StandardCharsets.UTF_8.toString())
}
String merge(List stringTestResults) {
String xmlShell = '' +
' ' +
' ' +
' ' +
' '
def testResults = stringTestResults.collect { new XmlParser().parseText(it) }
def firstTestResult = testResults.first()
def baseXml = new XmlParser().parseText(xmlShell)
baseXml.children().add(0, firstTestResult.environment.first())
baseXml.children().add(1, firstTestResult.'culture-info'.first())
def mergedResultsNode = baseXml.'test-suite'.results.first()
testResults.each { xml -> mergedResultsNode.append(xml.'test-suite') }
def attributes = ['total', 'errors', 'failures', 'not-run', 'inconclusive', 'skipped', 'invalid', 'ignored'];
attributes.each {
baseXml['@' + it] = testResults.inject(0) { r, node -> r + Integer.valueOf(node['@' + it] ?: 0) }
}
baseXml.@date = firstTestResult.@date
baseXml.@time = firstTestResult.@time
def mergedTestSuite = baseXml.'test-suite'
mergedTestSuite.@time = testResults.inject(0.0d) { r, node ->
r + Double.valueOf(node.'test-suite'.first().@time)
}
mergedTestSuite.@result = testResults.inject('Success') { r, node ->
r == 'Failure' ? r : node.'test-suite'.first().@result
}
return XmlUtil.serialize(baseXml)
}
}