-
Notifications
You must be signed in to change notification settings - Fork 0
/
junit_parser.rb
58 lines (53 loc) · 1.66 KB
/
junit_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true
require 'nokogiri'
require 'json'
require 'pathname'
require 'fileutils'
# parse JUnit Test results
class JunitParser
def self.parse(xml)
unless File.exist?(xml) && File.readable?(xml)
raise ArgumentError, "File #{xml} does not exist or is not readable"
end
doc = Nokogiri::XML(File.read(xml))
test_suites = []
doc.xpath('//testsuite').each do |test_suite|
device = test_suite.at_xpath('properties/property[@name="device"]')
device_name = device.nil? ? 'Unknown' : device['value']
suite = {
name: test_suite['name'],
path: File.dirname(xml),
filename: File.basename(xml),
tests: [],
count: test_suite['tests'].to_i,
failures: test_suite['failures'].to_i,
errors: test_suite['errors'].to_i,
skipped: test_suite['skipped'].to_i,
time: test_suite['time'].to_f,
device_name: device_name
}
test_suite.xpath('testcase').each do |test_case|
skipped = test_case.at_xpath('skipped') ? true : nil
failure = test_case.at_xpath('failure') ? test_case.at_xpath('failure').text : nil
error = test_case.at_xpath('error') ? test_case.at_xpath('error').text : nil
status = "Success"
if skipped
status = "Skipped"
end
if failure
status = "Failure"
end
suite[:tests] << {
name: test_case['name'],
time: test_case['time'].to_f,
classname: test_case['classname'],
status: status,
failure: failure,
error: error,
}
end
test_suites << suite
end
test_suites
end
end