Class: Xcode::Test::Formatters::JunitFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/xcode/test/formatters/junit_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ JunitFormatter

Returns a new instance of JunitFormatter.



8
9
10
11
# File 'lib/xcode/test/formatters/junit_formatter.rb', line 8

def initialize(dir)
  @dir = File.expand_path(dir).to_s
  FileUtils.mkdir_p(@dir)
end

Instance Method Details

#after_suite(suite) ⇒ Object



13
14
15
# File 'lib/xcode/test/formatters/junit_formatter.rb', line 13

def after_suite(suite)
  write(suite)
end

#write(report) ⇒ Object



17
18
19
20
21
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
# File 'lib/xcode/test/formatters/junit_formatter.rb', line 17

def write(report)
  if report.end_time.nil?
    raise "Report #{report} #{report.name} has a nil end time!?"
  end
  xml = ::Builder::XmlMarkup.new( :indent => 2 )
  xml.instruct! :xml, :encoding => "UTF-8"
  xml.testsuite(:errors     => report.total_errors,
    :failures   => report.total_failed_tests,
    :hostname   => Socket.gethostname,
    :name       => report.name,
    :tests      => report.tests.count,
    :time       => (report.end_time - report.start_time),
    :timestamp  => report.end_time
    ) do |p|     
             
    report.tests.each do |t|
      p.testcase(:classname  => report.name,
        :name       => t.name,
        :time       => t.time
        ) do |testcase|
  
        t.errors.each do |error|
          testcase.failure error[:location], :message => error[:message], :type => 'Failure'
        end
      end
    end
  end
  
  File.open(File.join(@dir, sanitize_filename("TEST-#{report.name}.xml")), 'w') do |current_file|
    current_file.write xml.target!
  end
  
end