Class: JUnitFormatter

Inherits:
YamlFormatter show all
Defined in:
lib/mspec/runner/formatters/junit.rb

Instance Attribute Summary

Attributes inherited from DottedFormatter

#exceptions, #tally, #timer

Instance Method Summary collapse

Methods inherited from YamlFormatter

#switch

Methods inherited from DottedFormatter

#abort, #before, #exception?, #failure?, #print, #print_exception, #register

Constructor Details

#initialize(out = nil) ⇒ JUnitFormatter


6
7
8
9
# File 'lib/mspec/runner/formatters/junit.rb', line 6

def initialize(out=nil)
  super
  @tests = []
end

Instance Method Details

#after(state = nil) ⇒ Object


11
12
13
14
# File 'lib/mspec/runner/formatters/junit.rb', line 11

def after(state = nil)
  super
  @tests << {:test => state, :exception => false} unless exception?
end

#exception(exception) ⇒ Object


16
17
18
19
# File 'lib/mspec/runner/formatters/junit.rb', line 16

def exception(exception)
  super
  @tests << {:test => exception, :exception => true}
end

#finishObject


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mspec/runner/formatters/junit.rb', line 21

def finish
  switch

  time = @timer.elapsed
  tests = @tally.counter.examples
  errors = @tally.counter.errors
  failures = @tally.counter.failures

  printf <<-XML

<?xml version="1.0" encoding="UTF-8" ?>
  <testsuites
      testCount="#{tests}"
      errorCount="#{errors}"
      failureCount="#{failures}"
      timeCount="#{time}" time="#{time}">
    <testsuite
        tests="#{tests}"
        errors="#{errors}"
        failures="#{failures}"
        time="#{time}"
        name="Spec Output For #{::RUBY_NAME} (#{::RUBY_VERSION})">
  XML
  @tests.each do |h|
    description = encode_for_xml h[:test].description

    printf <<-XML, "Spec", description, 0.0
      <testcase classname="%s" name="%s" time="%f">
    XML
    if h[:exception]
      outcome = h[:test].failure? ? "failure" : "error"
      message = encode_for_xml h[:test].message
      backtrace = encode_for_xml h[:test].backtrace
      print <<-XML
        <#{outcome} message="error in #{description}" type="#{outcome}">
          #{message}
          #{backtrace}
        </#{outcome}>
      XML
    end
    print <<-XML
      </testcase>
    XML
  end

  print <<-XML
    </testsuite>
  </testsuites>
  XML
end