Class: Cucumber::Formatter::Junit

Inherits:
Ast::Visitor show all
Defined in:
lib/cucumber/formatter/junit.rb

Instance Attribute Summary

Attributes inherited from Ast::Visitor

#options, #step_mother

Instance Method Summary collapse

Methods inherited from Ast::Visitor

#announce, #matches_scenario_names?, #visit_background_name, #visit_comment, #visit_comment_line, #visit_examples, #visit_examples_array, #visit_examples_name, #visit_exception, #visit_feature_element, #visit_features, #visit_multiline_arg, #visit_py_string, #visit_step, #visit_step_name, #visit_step_result, #visit_table_cell, #visit_table_cell_value, #visit_tag_name, #visit_tags

Constructor Details

#initialize(step_mother, io, options) ⇒ Junit

Returns a new instance of Junit.



7
8
9
10
11
12
# File 'lib/cucumber/formatter/junit.rb', line 7

def initialize(step_mother, io, options)
  super(step_mother)
  raise "You *must* specify --out DIR for the junit formatter" unless String === io && File.directory?(io)
  @reportdir = io
  @options = options
end

Instance Method Details

#visit_background(name) ⇒ Object



36
37
38
39
40
# File 'lib/cucumber/formatter/junit.rb', line 36

def visit_background(name)
  @in_background = true
  super
  @in_background = false
end

#visit_feature(feature) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cucumber/formatter/junit.rb', line 14

def visit_feature(feature)
  @failures = @errors = @tests = 0
  @builder = OrderedXmlMarkup.new( :indent => 2 )
  @time = 0
  super

  @testsuite = OrderedXmlMarkup.new( :indent => 2 )
  @testsuite.instruct!
  @testsuite.testsuite(
    :failures => @failures,
    :errors => @errors,
    :tests => @tests,
    :time => "%.6f" % @time,
  :name => @feature_name ) do
    @testsuite << @builder.target!
  end

  basename = File.basename(feature.file)[0...-File.extname(feature.file).length]
  feature_filename = File.join(@reportdir, "TEST-#{basename}.xml")
  File.open(feature_filename, 'w') { |file| file.write(@testsuite.target!) }
end

#visit_feature_name(name) ⇒ Object



42
43
44
45
# File 'lib/cucumber/formatter/junit.rb', line 42

def visit_feature_name(name)
  lines = name.split(/\r?\n/)
  @feature_name = lines[0].sub(/Feature\:/, '').strip
end

#visit_outline_table(outline_table) ⇒ Object



69
70
71
72
# File 'lib/cucumber/formatter/junit.rb', line 69

def visit_outline_table(outline_table)
  @header_row = true
  super(outline_table)
end

#visit_scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cucumber/formatter/junit.rb', line 47

def visit_scenario_name(keyword, name, file_colon_line, source_indent)
  scenario_name = name.strip
  scenario_name = "Unnamed scenario" if name == ""
  @scenario = scenario_name
  @outline = keyword.include?('Scenario Outline')
  @output = "Scenario#{ " outline" if @outline}: #{@scenario}\n\n"
end

#visit_steps(steps) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cucumber/formatter/junit.rb', line 55

def visit_steps(steps)
  return if @in_background
  start = Time.now
  super
  duration = Time.now - start
  unless @outline
    if steps.failed?
      steps.each { |step| @output += "#{step.keyword} #{step.name}\n" }
      @output += "\nMessage:\n"
    end
    build_testcase(duration, steps.status, steps.exception)
  end
end

#visit_table_row(table_row) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cucumber/formatter/junit.rb', line 74

def visit_table_row(table_row)
  if @outline
    start = Time.now
    super(table_row)
    duration = Time.now - start
    unless @header_row
      name_suffix = " (outline example : #{table_row.name})"
      if table_row.failed?
        @output += "Example row: #{table_row.name}\n"
        @output += "\nMessage:\n"
      end
      build_testcase(duration, table_row.status, table_row.exception,  name_suffix)
    end
  else
    super(table_row)
  end
  @header_row = false
end