Skip to content

Instantly share code, notes, and snippets.

@ajesler
Last active August 29, 2015 14:01
Show Gist options
  • Save ajesler/17bccb9167fa2f9a7512 to your computer and use it in GitHub Desktop.
Save ajesler/17bccb9167fa2f9a7512 to your computer and use it in GitHub Desktop.
Sort JUnit test results.
require 'nokogiri' # swap to using REXML - may be slower but its built in to the ruby standard lib.
require 'optparse'
######################################
# called like 'ruby junit-test-times.rb /home/user/project/target/surefire-reports'
######################################
class JUnitTestTimes
@options = {}
def initialize(args)
self.parse(args)
self.run
end
def parse(args)
@options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.separator ""
opts.separator "Common options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opt_parser.parse!(args)
@options[:dirs] = ARGV
end
def run
suites = []
testcases = []
@options[:dirs].each do |d|
process_dir(d, suites, testcases)
end
puts "@@@ SUITES @@@"
puts suites.sort { |a,b| b[:time] <=> a[:time] }
puts "@@@ TEST CASES @@@"
puts testcases.sort { |a,b| b[:time] <=> a[:time] }
end
def process_dir(dirName, suites, testcases)
Dir.glob(dirName+"/TEST-*.xml").each do |d|
process_test_results(d, suites, testcases)
end
end
def process_test_results(fileName, suiteResults, testcaseResults)
f = File.open(fileName)
doc = Nokogiri::XML(f)
f.close
testsuites = doc.xpath("/testsuite")
testsuites.each do |ts|
ts_name = ts['name']
ts_time = (ts['time'].to_f*1000).to_i
suiteResults << {:name => ts_name, :time => ts_time}
testcases = ts.xpath("//testcase")
testcases.each do |tc|
tc_time = (tc['time'].to_f*1000).to_i
testcaseResults << { :name => tc['name'], :time => tc_time, :suite => ts_name }
end
end
end
end
JUnitTestTimes.new(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment