Class: JUnitFormatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/rspec/core/formatters/junit_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ JUnitFormatter

Returns a new instance of JUnitFormatter.



8
9
10
11
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 8

def initialize(output)
  super(output)
  @test_results = { :failures => [], :successes => [] }
end

Instance Attribute Details

#test_resultsObject (readonly)

Returns the value of attribute test_results.



6
7
8
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 6

def test_results
  @test_results
end

Instance Method Details

#_xml_escape(x) ⇒ Object



65
66
67
68
69
70
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 65

def _xml_escape(x)
  x.gsub("&", "&").
    gsub("\"", """).
    gsub(">", ">").
    gsub("<", "&lt;")
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



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
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 39

def dump_summary(duration, example_count, failure_count, pending_count)
  super(duration, example_count, failure_count, pending_count)
  output.puts("<?xml version=\"1.0\" encoding=\"utf-8\" ?>")
  output.puts("<testsuite errors=\"0\" failures=\"#{failure_count+pending_count}\" tests=\"#{example_count}\" time=\"#{duration}\" timestamp=\"#{Time.now.iso8601}\">")
  output.puts("  <properties />")
  @test_results[:successes].each do |t|
    md          = t.
    runtime     = md[:execution_result][:run_time]
    description = _xml_escape(md[:full_description])
    file_path   = _xml_escape(md[:file_path])
    output.puts("  <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\" />")
  end
  @test_results[:failures].each do |t|
    md          = t.
    description = _xml_escape(md[:full_description])
    file_path   = _xml_escape(md[:file_path])
    runtime     = md[:execution_result][:run_time]
    output.puts("  <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\">")
    output.puts("    <failure message=\"failure\" type=\"failure\">")
    output.puts("<![CDATA[ #{extract_errors(t)} ]]>")
    output.puts("    </failure>")
    output.puts("  </testcase>")
  end
  output.puts("</testsuite>")
end

#example_failed(example) ⇒ Object



23
24
25
26
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 23

def example_failed(example)
  super(example)
  @test_results[:failures].push(example)
end

#example_passed(example) ⇒ Object



13
14
15
16
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 13

def example_passed(example)
  super(example)
  @test_results[:successes].push(example)
end

#example_pending(example) ⇒ Object



18
19
20
21
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 18

def example_pending(example)
  super(example)
  # let jenkins ignore this
end

#extract_errors(example) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec/core/formatters/junit_formatter.rb', line 28

def extract_errors(example)
  exception = example.[:execution_result][:exception_encountered] || example.[:execution_result][:exception]
  message = ""
  unless exception.nil?
    message  = exception.message
    message += "\n"
    message += format_backtrace(exception.backtrace, example).join("\n")
  end
  return message
end