Class: Spectre::Reporter::JUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/reporter/junit.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ JUnit

Returns a new instance of JUnit.



11
12
13
# File 'lib/spectre/reporter/junit.rb', line 11

def initialize config
  @config = config
end

Instance Method Details

#get_name(run_info) ⇒ Object



15
16
17
18
19
20
# File 'lib/spectre/reporter/junit.rb', line 15

def get_name run_info
  run_name = "[#{run_info.spec.name}] #{run_info.spec.subject.desc}"
  run_name += " - #{run_info.spec.context.__desc} -" unless run_info.spec.context.__desc.nil?
  run_name += " #{run_info.spec.desc}"
  run_name
end

#report(run_infos) ⇒ Object



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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spectre/reporter/junit.rb', line 22

def report run_infos
  now = Time.now.getutc
  timestamp = now.strftime('%s')
  datetime = now.strftime('%FT%T.%L')

  xml_str = '<?xml version="1.0" encoding="UTF-8" ?>'
  xml_str += '<testsuites>'

  suite_id = 0

  run_infos.group_by { |x| x.spec.subject }.each do |subject, info_group|
    failures = info_group.select { |x| x.failure != nil }
    errors = info_group.select { |x| x.error != nil }
    skipped = info_group.select { |x| x.skipped? }

    xml_str += %{<testsuite package="#{CGI::escapeHTML(subject.desc)}" id="#{CGI::escapeHTML(suite_id.to_s)}" name="#{CGI::escapeHTML(subject.desc)}" timestamp="#{datetime}" tests="#{run_infos.count}" failures="#{failures.count}" errors="#{errors.count}" skipped="#{skipped.count}">}
    suite_id += 1

    info_group.each do |run_info|
      started = run_info.started.strftime('%FT%T.%L')

      xml_str += %{<testcase classname="#{CGI::escapeHTML(run_info.spec.file.to_s)}" name="#{get_name(run_info)}" timestamp="#{started}"  time="#{('%.3f' % run_info.duration)}">}

      if run_info.failure and !run_info.failure.cause
        failure_message = "Expected #{run_info.failure.expectation}"
        failure_message += " with #{run_info.data}" if run_info.data
        failure_message += " but it failed"
        failure_message += " with message: #{run_info.failure.message}" if run_info.failure.message

        xml_str += %{<failure message="#{CGI::escapeHTML(failure_message.gsub('"', '`'))}"></failure>}
      end


      if run_info.error or (run_info.failure and run_info.failure.cause)
        error = run_info.error || run_info.failure.cause

        type = error.class.name
        failure_message = error.message
        text = error.backtrace.join "\n"

        xml_str += %{<error message="#{CGI::escapeHTML(failure_message.gsub('"', '`'))}" type="#{type}">}
        xml_str += "<![CDATA[#{text}]]>"
        xml_str += '</error>'
      end


      if run_info.log.count > 0 or run_info.properties.count > 0 or run_info.data
        xml_str += '<system-out>'
        xml_str += '<![CDATA['

        if run_info.properties.count > 0
          run_info.properties.each do |key, val|
            xml_str += "#{key}: #{val}\n"
          end
        end

        if run_info.data
          data_str = run_info.data
          data_str = run_info.data.to_json unless run_info.data.is_a? String or run_info.data.is_a? Integer
          xml_str += "data: #{data_str}\n"
        end

        if run_info.log.count > 0
          messages = run_info.log.map { |x| "[#{x[0].strftime('%F %T')}] #{x[1]}" }
          xml_str += messages.join("\n")
        end

        xml_str += ']]>'
        xml_str += '</system-out>'
      end

      xml_str += '</testcase>'
    end

    xml_str += '</testsuite>'
  end

  xml_str += '</testsuites>'

  Dir.mkdir @config['out_path'] unless Dir.exist? @config['out_path']

  file_path = File.join(@config['out_path'], "spectre-junit_#{timestamp}.xml")

  File.write(file_path, xml_str)
end