Skip to content

Instantly share code, notes, and snippets.

@tjh
Created August 12, 2010 19:39
Show Gist options
  • Save tjh/521581 to your computer and use it in GitHub Desktop.
Save tjh/521581 to your computer and use it in GitHub Desktop.
# -----------------------------------------
# Cucumber formatter that outputs the file
# and line number for each test.
#
# Useful for Hudson CI which only flushes
# the output to the log after a line return
# so that you can see what's failed before
# a long build finishes.
# -----------------------------------------
#
# Usage
#
# - Put the contents of this file into "features/support/oneline.rb"
#
# - Make sure cucumber has oneline.rb in the load list (which is the
# default unless you have "-r" parameters in your cucumber.yml)
#
# - Add "--format Oneline" to your cucumber.yml or at the command line
#
# Example output
#
# [Thu, 12 Aug 2010 15:30:08 -0400] Pass features/admin/_login.feature:L#8
# [Thu, 12 Aug 2010 15:30:09 -0400] **** FAIL **** features/admin/_login.feature:L#12
# [Thu, 12 Aug 2010 15:30:10 -0400] Pass features/admin/_login.feature:L#19
# [Thu, 12 Aug 2010 15:30:11 -0400] Pass features/admin/_login.feature:L#29
#
require 'cucumber/formatter/io'
require 'cucumber/formatter/console'
class Oneline
include Cucumber::Formatter::Io
include Cucumber::Formatter::Console
attr_reader :step_mother
def initialize(step_mother, path_or_io, options)
@step_mother, @io, @options = step_mother, ensure_io(path_or_io, "oneline"), options
@file_names = []
@file_colon_lines = Hash.new{|h,k| h[k] = []}
end
def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
if status != :skipped
@step_details << "#{keyword} #{step_match.format_args}"
end
if ![:passed, :skipped].include?(status)
@step_details << ""
@step_details << "#{exception}"
@step_details << "#{step_match.backtrace_line}"
end
end
def before_feature_element(feature_element)
reset_scenario_status
end
def after_feature_element(feature_element)
# Don't print the sum result of an outline scenario
unless @scenario_outline
file, line = feature_element.file_colon_line.split(':')
progress(@scenario_status, "#{file}:L##{line}")
if @scenario_status != :passed && @step_details.size > 0
@io.print("\n")
@io.print(format_string("\n\n Scenario steps:\n", @scenario_status))
@step_details.each do |step|
@io.print(format_string("\n #{step}", @scenario_status))
end
@io.print("\n")
@io.flush
end
end
@scenario_outline = false
end
def step_name(keyword, step_match, status, source_indent, background)
update_scenario_status(status)
end
def after_table_row(table_row)
if table_row && table_row.respond_to?("passed?")
begin
table_row.passed?
file, line = table_row.backtrace_line.split(':')
progress(table_row.status, "#{file}:L##{line}")
update_scenario_status(table_row.status)
rescue Cucumber::Ast::OutlineTable::ExampleRow::InvalidForHeaderRowError
# Header row for a scenario outline
@scenario_outline = true
end
end
reset_scenario_status
end
def after_features(features)
@io.puts
@io.puts
print_summary(features)
end
private
def reset_scenario_status
@scenario_status = :passed
@step_details = []
end
def update_scenario_status(status)
@scenario_status = status if status == :failed
@scenario_status = status unless [:failed, :pending, :undefined].include?(@scenario_status)
end
def progress(status, message = "")
char = CHARS[status]
@io.print(format_string("\n[#{Time.now.rfc822}] #{char} #{message}", status))
@io.flush
end
CHARS = {
:passed => " Pass ",
:failed => "**** FAIL ****",
:undefined => "**** Undefined",
:pending => "**** Pending ",
:skipped => " Skipped "
}
def print_summary(features)
print_steps(:pending)
print_steps(:failed)
print_stats(features, @options.custom_profiles)
print_snippets(@options)
print_passing_wip(@options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment