Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Created March 7, 2013 09:12
Show Gist options
  • Save chetanmeh/5106656 to your computer and use it in GitHub Desktop.
Save chetanmeh/5106656 to your computer and use it in GitHub Desktop.
Script to perform diff between two Intellij test run result which are exported as xml
/**
* Utility class that diffs 2 Intellij test runs and performs diff between the two runs. It works
* on xml report created by Intellij as mentioned in http://www.jetbrains.com/idea/webhelp/export-test-results.html
*
* How to run
*
* - Run first test suite and export the result as xml
* - Run first test suite again post change and export the result as xml
*
* Run the script
* groovy diffIntellijTestResult.groovy result1.xml result2.xml
*
* It would produce a table of diff
*/
def cli = new CliBuilder(
usage: 'diffTestResult file1 file2',
header: '\nAvailable options (use -h for help):\n',
footer: '\nPerforms diff of the test result\n',
posix: true
)
cli.with {
h(longOpt: 'help', 'Usage Information', required: false)
}
opt = cli.parse(args)
if (!opt) return
if (opt.h) {cli.usage(); return}
def arguments = opt.arguments()
assert arguments.size() == 2, "two files should be provided in input"
def r1 = parseSuites(parseFile(arguments[0]).suite,[:])
def r2 = parseSuites(parseFile(arguments[1]).suite,[:])
def comparison = []
r1.each{String name, TestInfo ti1 ->
TestInfo ti2 = r2[name]
if (ti1.status != ti2?.status){
comparison << [name:name, first:ti1, second:ti2]
}
}
println "||Test||First Run||Second Run||"
comparison.each{c ->
println "|$c.name|$c.first.status|$c.second.status|"
}
def parseSuites(def suite,def resultMap){
def suiteName = suite.@name.text()
suite.test.each{t ->
def ti = new TestInfo(suiteName:suiteName,methodName:t.@name.text(),status: t.@status.text() )
resultMap[ti.name()] = ti
}
suite.suite.each{s ->
parseSuites(s,resultMap)
}
return resultMap
}
def parseFile(def path){
File f = new File(path)
assert f.exists(), "File $f does not exist"
String text = f.text
//Need to fix the output as the stdout/stderr might contain invalid chars
//wrap them in cdata
text = text.replace('<output type="stderr">','<output type="stderr"><![CDATA[')
text = text.replace('<output type="stdout">','<output type="stdout"><![CDATA[')
text = text.replace('</output>',']]></output>')
return new XmlSlurper().parseText(text)
}
class TestInfo {
String suiteName
String status
String methodName
int duration
def name(){
suiteName +'.'+methodName
}
@Override
public String toString() {
return name() + '-' + status
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment