Class: Cucumber::Formatter::Junit

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/formatter/junit.rb

Overview

The formatter used for --format junit

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, io, options) ⇒ Junit

Returns a new instance of Junit.



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

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

Instance Method Details

#after_background(*args) ⇒ Object



40
41
42
# File 'lib/cucumber/formatter/junit.rb', line 40

def after_background(*args)
  @in_background = false
end

#after_feature(feature) ⇒ Object



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

def after_feature(feature)
  @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

#after_steps(steps) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cucumber/formatter/junit.rb', line 61

def after_steps(steps)
  return if @in_background
  duration = Time.now - @steps_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

#after_table_row(table_row) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cucumber/formatter/junit.rb', line 85

def after_table_row(table_row)
  if @outline
    duration = Time.now - @table_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
  end
  @header_row = false
end

#before_background(*args) ⇒ Object



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

def before_background(*args)
  @in_background = true
end

#before_feature(feature) ⇒ Object



13
14
15
16
17
# File 'lib/cucumber/formatter/junit.rb', line 13

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

#before_outline_table(outline_table) ⇒ Object



73
74
75
# File 'lib/cucumber/formatter/junit.rb', line 73

def before_outline_table(outline_table)
  @header_row = true
end

#before_steps(steps) ⇒ Object



57
58
59
# File 'lib/cucumber/formatter/junit.rb', line 57

def before_steps(steps)
  @steps_start = Time.now
end

#before_table_row(table_row) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/cucumber/formatter/junit.rb', line 77

def before_table_row(table_row)
  if @outline
    @table_start = Time.now
  end
  
  @header_row = false
end

#feature_name(name) ⇒ Object



44
45
46
47
# File 'lib/cucumber/formatter/junit.rb', line 44

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

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



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

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