Class: ReportPortal::RSpec::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/report_portal/rspec/formatter.rb

Constant Summary collapse

MAX_DESCRIPTION_LENGTH =
255
MIN_DESCRIPTION_LENGTH =
3

Instance Method Summary collapse

Constructor Details

#initialize(_output) ⇒ Formatter

Returns a new instance of Formatter.



37
38
39
# File 'lib/report_portal/rspec/formatter.rb', line 37

def initialize(_output)
  ENV['REPORT_PORTAL_USED'] = 'true'
end

Instance Method Details

#example_failed(notification) ⇒ Object



105
106
107
108
109
110
# File 'lib/report_portal/rspec/formatter.rb', line 105

def example_failed(notification)
  exception = notification.exception
  ReportPortal.send_log(:failed, %(#{exception.class}: #{exception.message}\n\nStacktrace: #{notification.formatted_backtrace.join("\n")}), ReportPortal.now)
  ReportPortal.finish_item(ReportPortal.current_scenario, :failed) unless ReportPortal.current_scenario.nil?
  ReportPortal.current_scenario = nil
end

#example_group_finished(_group_notification) ⇒ Object



71
72
73
74
75
76
# File 'lib/report_portal/rspec/formatter.rb', line 71

def example_group_finished(_group_notification)
  unless @current_group_node.nil?
    ReportPortal.finish_item(@current_group_node.content)
    @current_group_node = @current_group_node.parent
  end
end

#example_group_started(group_notification) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/report_portal/rspec/formatter.rb', line 48

def example_group_started(group_notification)
  description = group_notification.group.description
  if description.size < MIN_DESCRIPTION_LENGTH
    p "Group description should be at least #{MIN_DESCRIPTION_LENGTH} characters ('group_notification': #{group_notification.inspect})"
    return
  end
  item = ReportPortal::TestItem.new(description[0..MAX_DESCRIPTION_LENGTH-1],
                                    :TEST,
                                    nil,
                                    ReportPortal.now,
                                    '',
                                    false,
                                    [])
  group_node = Tree::TreeNode.new(SecureRandom.hex, item)
  if group_node.nil?
    p "Group node is nil for item #{item.inspect}"
  else
    @current_group_node << group_node unless @current_group_node.nil? # make @current_group_node parent of group_node

    @current_group_node = group_node
    group_node.content.id = ReportPortal.start_item(group_node)
  end
end

#example_passed(_notification) ⇒ Object



100
101
102
103
# File 'lib/report_portal/rspec/formatter.rb', line 100

def example_passed(_notification)
  ReportPortal.finish_item(ReportPortal.current_scenario, :passed) unless ReportPortal.current_scenario.nil?
  ReportPortal.current_scenario = nil
end

#example_pending(_notification) ⇒ Object



112
113
114
115
# File 'lib/report_portal/rspec/formatter.rb', line 112

def example_pending(_notification)
  ReportPortal.finish_item(ReportPortal.current_scenario, :skipped) unless ReportPortal.current_scenario.nil?
  ReportPortal.current_scenario = nil
end

#example_started(notification) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/report_portal/rspec/formatter.rb', line 78

def example_started(notification)
  description = notification.example.description
  if description.size < MIN_DESCRIPTION_LENGTH
    p "Example description should be at least #{MIN_DESCRIPTION_LENGTH} characters ('notification': #{notification.inspect})"
    return
  end
  ReportPortal.current_scenario = ReportPortal::TestItem.new(description[0..MAX_DESCRIPTION_LENGTH-1],
                                                             :STEP,
                                                             nil,
                                                             ReportPortal.now,
                                                             '',
                                                             false,
                                                             [])
  example_node = Tree::TreeNode.new(SecureRandom.hex, ReportPortal.current_scenario)
  if example_node.nil?
    p "Example node is nil for scenario #{ReportPortal.current_scenario.inspect}"
  else
    @current_group_node << example_node
    example_node.content.id = ReportPortal.start_item(example_node)
  end
end

#message(notification) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/report_portal/rspec/formatter.rb', line 117

def message(notification)
  if notification.message.respond_to?(:read)
    ReportPortal.send_file(:passed, notification.message)
  else
    ReportPortal.send_log(:passed, notification.message, ReportPortal.now)
  end
end

#start(_start_notification) ⇒ Object



41
42
43
44
45
46
# File 'lib/report_portal/rspec/formatter.rb', line 41

def start(_start_notification)
  cmd_args = ARGV.map { |arg| (arg.include? 'rp_uuid=')? 'rp_uuid=[FILTERED]' : arg }.join(' ')
  ReportPortal.start_launch(cmd_args)
  @root_node = Tree::TreeNode.new(SecureRandom.hex)
  @current_group_node = @root_node
end

#stop(_notification) ⇒ Object



125
126
127
# File 'lib/report_portal/rspec/formatter.rb', line 125

def stop(_notification)
  ReportPortal.finish_launch
end