Class: Cucumber::Formatter::Junit

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

Overview

The formatter used for --format junit

Defined Under Namespace

Classes: UnNamedFeatureError

Constant Summary

AST_SCENARIO_OUTLINE =

 TODO: remove coupling to types

Core::Ast::ScenarioOutline
AST_EXAMPLE_ROW =
LegacyApi::Ast::ExampleTableRow

Instance Method Summary collapse

Methods included from Io

#ensure_dir, #ensure_file, #ensure_io

Constructor Details

#initialize(runtime, io, options) ⇒ Junit



23
24
25
26
# File 'lib/cucumber/formatter/junit.rb', line 23

def initialize(runtime, io, options)
  @reportdir = ensure_dir(io, "junit")
  @options = options
end

Instance Method Details

#after_background(*args) ⇒ Object



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

def after_background(*args)
  @in_background = false
end

#after_examples(*args) ⇒ Object



107
108
109
# File 'lib/cucumber/formatter/junit.rb', line 107

def after_examples(*args)
  @in_examples = false
end

#after_feature(feature) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cucumber/formatter/junit.rb', line 44

def after_feature(feature)
  @testsuite = Builder::XmlMarkup.new( :indent => 2 )
  @testsuite.instruct!
  @testsuite.testsuite(
    :failures => @failures,
    :errors => @errors,
    :skipped => @skipped,
    :tests => @tests,
    :time => "%.6f" % @time,
    :name => @feature_name ) do
    @testsuite << @builder.target!
    @testsuite.tag!('system-out') do
      @testsuite.cdata! strip_control_chars(@interceptedout.buffer.join)
    end
    @testsuite.tag!('system-err') do
      @testsuite.cdata! strip_control_chars(@interceptederr.buffer.join)
    end
  end

  write_file(feature_result_filename(feature.file), @testsuite.target!)

  Interceptor::Pipe.unwrap! :stdout
  Interceptor::Pipe.unwrap! :stderr
end

#after_steps(steps) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/cucumber/formatter/junit.rb', line 91

def after_steps(steps)
  return if @in_background || @in_examples

  duration = Time.now - @steps_start
  if steps.failed?
    steps.each { |step| @output += "#{step.keyword}#{step.name}\n" }
    @output += "\nMessage:\n"
  end
  build_testcase(duration, steps.status, steps.exception)
end

#after_table_row(table_row) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cucumber/formatter/junit.rb', line 117

def after_table_row(table_row)
  return unless @in_examples and AST_EXAMPLE_ROW === table_row
  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

  @header_row = false if @header_row
end

#before_background(*args) ⇒ Object



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

def before_background(*args)
  @in_background = true
end

#before_examples(*args) ⇒ Object



102
103
104
105
# File 'lib/cucumber/formatter/junit.rb', line 102

def before_examples(*args)
  @header_row = true
  @in_examples = true
end

#before_feature(feature) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/cucumber/formatter/junit.rb', line 28

def before_feature(feature)
  @current_feature = feature
  @failures = @errors = @tests = @skipped = 0
  @builder = Builder::XmlMarkup.new( :indent => 2 )
  @time = 0
  # In order to fill out <system-err/> and <system-out/>, we need to
  # intercept the $stderr and $stdout
  @interceptedout = Interceptor::Pipe.wrap(:stdout)
  @interceptederr = Interceptor::Pipe.wrap(:stderr)
end

#before_feature_element(feature_element) ⇒ Object



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

def before_feature_element(feature_element)
  @in_examples = AST_SCENARIO_OUTLINE === feature_element
  @steps_start = Time.now
end

#before_steps(steps) ⇒ Object



88
89
# File 'lib/cucumber/formatter/junit.rb', line 88

def before_steps(steps)
end

#before_table_row(table_row) ⇒ Object



111
112
113
114
115
# File 'lib/cucumber/formatter/junit.rb', line 111

def before_table_row(table_row)
  return unless @in_examples

  @table_start = Time.now
end

#feature_name(keyword, name) ⇒ Object



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

def feature_name(keyword, name)
  raise UnNamedFeatureError.new(@current_feature.file) if name.empty?
  lines = name.split(/\r?\n/)
  @feature_name = lines[0]
end

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



83
84
85
86
# File 'lib/cucumber/formatter/junit.rb', line 83

def scenario_name(keyword, name, file_colon_line, source_indent)
  @scenario = (name.nil? || name == "") ? "Unnamed scenario" : name.split("\n")[0]
  @output = "#{keyword}: #{@scenario}\n\n"
end