Method: CukeSniffer::Formatter.output_junit_xml

Defined in:
lib/cuke_sniffer/formatter.rb

.output_junit_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object

Creates an xml file that can be read by Jenkins/Hudson in the junit format with issues organized and collated by file. Each file becomes a testsuite with corresponding failures associated to it. If no failures are found this will be marked as a pass by Jenkins/Hudson. file_name defaults to “cuke_sniffer_result.xml” unless specified

cuke_sniffer.output_xml

Or

cuke_sniffer.output_xml("cuke_sniffer01-01-0001.xml")


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cuke_sniffer/formatter.rb', line 114

def self.output_junit_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME)
  file_name = file_name + ".xml" unless file_name =~ /\.xml$/
  results = {}
  failures = 0
  suits={}
  current = cuke_sniffer.features
  current.concat cuke_sniffer.scenarios
  current.concat cuke_sniffer.step_definitions
  current.concat cuke_sniffer.hooks
  current.each do |test|
    location = test.location.gsub("#{Dir.pwd}/", '')
    location_no_line = location.gsub(/:[0-9]*/, '')
    line_num = location.include?(":") ? location.gsub(/.*:(.*)/, "\\1") : "full_file"
    errors = test.rules_hash.keys.map { |f| {:line => "line: #{line_num}",
                                             :error => f,
                                             :formatted => "Location: #{location}",
                                             :instances => "Instances: #{test.rules_hash[f]}"
    } }
    results[location_no_line] = results[location_no_line].nil? ? errors : results[location_no_line].concat(errors)
    failures += test.rules_hash.size
  end
  results.each do |location, failure|
    suits[location]=failure
  end
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.testsuites(:tests => results.size, :failures => failures) do
      suits.each do |location, failures|
        xml.testsuite(:name => location, :tests => 1, :failures => failures.length) do
          if failures.length == 0
            xml.testcase(:classname => location, :name => location, :time => 0, :status => 0)
          else
            failures.each do |failure|
              xml.testcase(:classname => location, :name => failure[:line], :time => 0, :status => failure[:instances]) do
                xml.failure(failure[:formatted], :type => 'failure', :message => "#{failure[:error]} #{failure[:instances]}")
              end
            end
          end
        end
      end
    end
  end
  output = builder.to_xml
  File.open(file_name, 'w') do |f|
    f.write(output)
  end
  # Return here to aid testing.
  output
end