Class: RSpec::TestSpec::Formatter

Inherits:
Core::Formatters::DocumentationFormatter
  • Object
show all
Defined in:
lib/test_spec/rspec/formatter.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

DEFAULT_REPORT_PATH =
File.join(
  '.', 'reports', Time.now.strftime('%Y%m%d-%H%M%S')
)
REPORT_PATH =
ENV['REPORT_PATH'] || DEFAULT_REPORT_PATH
SCREENRECORD_DIR =
File.join(REPORT_PATH, 'screenrecords')
SCREENSHOT_DIR =
File.join(REPORT_PATH, 'screenshots')

Instance Method Summary collapse

Constructor Details

#initialize(_output) ⇒ Formatter

Returns a new instance of Formatter.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/test_spec/rspec/formatter.rb', line 29

def initialize(_output)
  super
  create_report_directory
  create_screenshots_directory
  create_screenrecords_directory
  provide_resources

  @all_groups = {}

  # REPEATED FROM THE SUPER
  # @group_level = 0
end

Instance Method Details

#close(notification) ⇒ Object

This is from BaseTextFormatter. rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/test_spec/rspec/formatter.rb', line 193

def close(notification)
  File.open("#{REPORT_PATH}/overview.html", "w") do |f|
    @overview = @all_groups

    @passed = @overview.values.map { |g| g[:passed].size }.inject(0) { |sum, i| sum + i }
    @failed = @overview.values.map { |g| g[:failed].size }.inject(0) { |sum, i| sum + i }
    @pending = @overview.values.map { |g| g[:pending].size }.inject(0) { |sum, i| sum + i }

    duration_values = @overview.values.map { |e| e[:duration] }
    duration_keys = duration_values.size.times.to_a

    if duration_values.size < 2
      duration_values.unshift(duration_values.first)
      duration_keys = duration_keys << 1
    end

    @durations = duration_keys.zip(duration_values.map { |d| d.to_f.round(5) })
    @summary_duration = duration_values.map { |d| d.to_f.round(5) }.inject(0) { |sum, i| sum + i }.to_s(:rounded, precision: 5)
    @total_examples = @passed + @failed + @pending

    template_file = File.read(
      File.dirname(__FILE__) + "/../../../templates/overview.erb"
    )

    f.puts ERB.new(template_file).result(binding)
  end

  super
end

#example_failed(notification) ⇒ Object

This comes from the DocumentationFormatter.



63
64
65
66
# File 'lib/test_spec/rspec/formatter.rb', line 63

def example_failed(notification)
  @group_example_failure_count += 1
  @examples << Example.new(notification.example)
end

#example_group_finished(notification) ⇒ Object

rubocop:disable Metrics/LineLength rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/test_spec/rspec/formatter.rb', line 123

def example_group_finished(notification)
  super

  return unless @group_level.zero?

  # rubocop:disable Metrics/BlockLength
  File.open("#{REPORT_PATH}/#{notification.group.description.parameterize}.html", "w") do |f|
    @passed = @group_example_success_count
    @failed = @group_example_failure_count
    @pending = @group_example_pending_count

    duration_values = @examples.map(&:run_time)
    duration_keys = duration_values.size.times.to_a

    if duration_values.size < 2 && !duration_values.empty?
      duration_values.unshift(duration_values.first)
      duration_keys = duration_keys << 1
    end

    @title = notification.group.description
    @durations = duration_keys.zip(duration_values)
    @summary_duration = duration_values.inject(0) { |sum, i| sum + i }.to_s(:rounded, precision: 5)

    Example.load_spec_comments!(@examples)

    class_map = {
      passed: 'success',
      failed: 'danger',
      pending: 'warning'
    }

    statuses = @examples.map(&:status)

    status =
      if statuses.include?('failed')
        'failed'
      elsif statuses.include?('passed')
        'passed'
      else
        'pending'
      end

    @all_groups[notification.group.description.parameterize] = {
      group: notification.group.description,
      examples: @examples.size,
      status: status,
      klass: class_map[status.to_sym],
      passed: statuses.select { |s| s == 'passed' },
      failed: statuses.select { |s| s == 'failed' },
      pending: statuses.select { |s| s == 'pending' },
      duration: @summary_duration
    }

    template_file = File.read(
      File.dirname(__FILE__) + "/../../../templates/report.erb"
    )

    f.puts ERB.new(template_file).result(binding)
  end
  # rubocop:enable Metrics/BlockLength

  # THIS ONE IS FROM THE SUPER
  # @group_level -= 1 if @group_level > 0
end

#example_group_started(_notification) ⇒ Object

ADDED FOR REPORTING



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/test_spec/rspec/formatter.rb', line 105

def example_group_started(_notification)
  if @group_level.zero?
    @examples = []
    @group_example_count = 0
    @group_example_success_count = 0
    @group_example_failure_count = 0
    @group_example_pending_count = 0
  end

  super

  # REPEATED FROM THE SUPER
  # @group_level += 1
end

#example_passed(notification) ⇒ Object

This comes from the DocumentationFormatter.



54
55
56
57
58
59
60
# File 'lib/test_spec/rspec/formatter.rb', line 54

def example_passed(notification)
  super unless notification.example.[:with_steps]

  # For reporter
  @group_example_success_count += 1
  @examples << Example.new(notification.example)
end

#example_pending(notification) ⇒ Object

This comes from the DocumentationFormatter. Needed for Reporter.



70
71
72
73
# File 'lib/test_spec/rspec/formatter.rb', line 70

def example_pending(notification)
  @group_example_pending_count += 1
  @examples << Example.new(notification.example)
end

#example_started(notification) ⇒ Object

rubocop:disable Metrics/LineLength



43
44
45
46
47
48
49
50
51
# File 'lib/test_spec/rspec/formatter.rb', line 43

def example_started(notification)
  return unless notification.example.[:with_steps]

  full_message = "#{current_indentation}#{notification.example.description}"
  output.puts Core::Formatters::ConsoleCodes.wrap(full_message, :default)

  # For reporter
  @group_example_count += 1
end

#example_step_failed(notification) ⇒ Object

rubocop:enable Metrics/AbcSize rubocop:enable Style/ConditionalAssignment



97
98
99
100
# File 'lib/test_spec/rspec/formatter.rb', line 97

def example_step_failed(notification)
  full_message = "#{current_indentation}  #{notification.type.to_s.capitalize} #{notification.message} (FAILED)"
  output.puts Core::Formatters::ConsoleCodes.wrap(full_message, :failure)
end

#example_step_passed(notification) ⇒ Object



75
76
77
78
# File 'lib/test_spec/rspec/formatter.rb', line 75

def example_step_passed(notification)
  full_message = "#{current_indentation}  #{notification.type.to_s.capitalize} #{notification.message}"
  output.puts Core::Formatters::ConsoleCodes.wrap(full_message, :success)
end

#example_step_pending(notification) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Style/ConditionalAssignment



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/test_spec/rspec/formatter.rb', line 82

def example_step_pending(notification)
  full_message = "#{current_indentation}  #{notification.type.to_s.capitalize} #{notification.message}"

  if notification.options[:pending] &&
     notification.options[:pending] != true
    full_message << " (PENDING: #{notification.options[:pending]})"
  else
    full_message << " (PENDING)"
  end

  output.puts Core::Formatters::ConsoleCodes.wrap(full_message, :pending)
end