Method: PDK::Report#write_junit

Defined in:
lib/pdk/report.rb

#write_junit(target = self.class.default_target) ⇒ Object

Renders the report as a JUnit XML document.

Parameters:

  • target (#write) (defaults to: self.class.default_target)

    an IO object that the report will be written to. Defaults to PDK::Report.default_target.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pdk/report.rb', line 49

def write_junit(target = self.class.default_target)
  require 'rexml/document'
  require 'time'
  require 'socket'

  document = REXML::Document.new
  document << REXML::XMLDecl.new
  testsuites = REXML::Element.new('testsuites')

  id = 0
  events.each do |testsuite_name, testcases|
    testsuite = REXML::Element.new('testsuite')
    testsuite.attributes['name'] = testsuite_name
    testsuite.attributes['tests'] = testcases.length
    testsuite.attributes['errors'] = testcases.select(&:error?).length
    testsuite.attributes['failures'] = testcases.select(&:failure?).length
    testsuite.attributes['skipped'] = testcases.select(&:skipped?).length
    testsuite.attributes['time'] = 0
    testsuite.attributes['timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
    testsuite.attributes['hostname'] = Socket.gethostname
    testsuite.attributes['id'] = id
    testsuite.attributes['package'] = testsuite_name
    testsuite.add_element('properties')
    testcases.each { |r| testsuite.elements << r.to_junit }
    testsuite.add_element('system-out')
    testsuite.add_element('system-err')

    testsuites.elements << testsuite
    id += 1
  end

  document.elements << testsuites
  report = ''
  document.write(report, 2)

  if target.is_a?(String)
    PDK::Util::Filesystem.write_file(target, report)
  else
    target << report
  end
end