Class: JUnitFormatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ JUnitFormatter

Returns a new instance of JUnitFormatter.



36
37
38
39
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 36

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

Instance Attribute Details

#test_resultsObject (readonly)

Returns the value of attribute test_results.



34
35
36
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 34

def test_results
  @test_results
end

Instance Method Details

#_xml_escape(x) ⇒ Object



102
103
104
105
106
107
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 102

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

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



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

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}\" skipped=\"#{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[ #{read_failure(t)} ]]>")
    output.puts("    </failure>")
    output.puts("  </testcase>")
  end
  @test_results[:skipped].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("    <skipped/>")
    output.puts("  </testcase>")
  end
  output.puts("</testsuite>")
end

#example_failed(example) ⇒ Object



51
52
53
54
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 51

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

#example_passed(example) ⇒ Object



41
42
43
44
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 41

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

#example_pending(example) ⇒ Object



46
47
48
49
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 46

def example_pending(example)
  super(example)
  @test_results[:skipped].push(example)
end

#read_failure(t) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec-extra-formatters/junit_formatter.rb', line 56

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