class Turn::PrettyReporter

Pretty Reporter (by Paydro)

Example output:

TestCaseName:
     PASS test: Succesful test case.  (0:00:02:059)
    ERROR test: Bogus test case.  (0:00:02:059)
     FAIL test: Failed test case.  (0:00:02:059)

Constants

TAB_SIZE

Second column left padding in chars.

TRACE_MARK

Character to put in front of backtrace.

Public Instance Methods

error(exception, message=nil) click to toggle source

Invoked when a test raises an exception.

# File lib/turn/reporters/pretty_reporter.rb, line 69
def error(exception, message=nil)
  banner ERROR

  prettify(exception, message)
end
fail(assertion, message=nil) click to toggle source

Invoked when a test raises an assertion.

# File lib/turn/reporters/pretty_reporter.rb, line 62
def fail(assertion, message=nil)
  banner FAIL

  prettify(assertion, message)
end
finish_case(kase) click to toggle source

Invoked after all tests in a testcase have ben run.

# File lib/turn/reporters/pretty_reporter.rb, line 83
def finish_case(kase)
  # Print newline is there any tests in suite
  io.puts if kase.size > 0
end
finish_suite(suite) click to toggle source

After all tests are run, this is the last observable action.

# File lib/turn/reporters/pretty_reporter.rb, line 89
def finish_suite(suite)
  total      = colorize_count("%d tests", suite.count_tests, :bold)
  passes     = colorize_count("%d passed", suite.count_passes, :pass)
  assertions = colorize_count("%d assertions", suite.count_assertions, nil)
  failures   = colorize_count("%d failures", suite.count_failures, :fail)
  errors     = colorize_count("%d errors", suite.count_errors, :error)
  skips      = colorize_count("%d skips", suite.count_skips, :skip)

  io.puts "Finished in %.6f seconds." % (Time.now - @time)
  io.puts

  io.puts [ total, passes, failures, errors, skips, assertions ].join(", ")

  # Please keep this newline, since it will be useful when after test case
  # there will be other lines. For example "rake aborted!" or kind of.
  io.puts
end
pass(message=nil) click to toggle source

Invoked when a test passes.

# File lib/turn/reporters/pretty_reporter.rb, line 50
def pass(message=nil)
  banner PASS

  if message
    message = Colorize.magenta(message)
    message = message.to_s.tabto(TAB_SIZE)

    io.puts(message)
  end
end
skip(exception, message=nil) click to toggle source

Invoked when a test is skipped.

# File lib/turn/reporters/pretty_reporter.rb, line 76
def skip(exception, message=nil)
  banner SKIP

  prettify(exception, message)
end
start_case(kase) click to toggle source

Invoked before a testcase is run.

# File lib/turn/reporters/pretty_reporter.rb, line 37
def start_case(kase)
  # Print case name if there any tests in suite
  # TODO: Add option which will show all test cases, even without tests?
  io.puts kase.name if kase.size > 0
end
start_suite(suite) click to toggle source

At the very start, before any testcases are run, this is called.

# File lib/turn/reporters/pretty_reporter.rb, line 22
def start_suite(suite)
  @suite  = suite
  @time   = Time.now

  io.puts Colorize.bold("Loaded Suite #{suite.name}")
  io.puts
  if suite.seed
    io.puts "Started at #{Time.now} w/ seed #{suite.seed}."
  else
    io.puts "Started at #{Time.now}."
  end
  io.puts
end
start_test(test) click to toggle source

Invoked before a test is run.

# File lib/turn/reporters/pretty_reporter.rb, line 44
def start_test(test)
  @test_time = Time.now
  @test = test
end

Private Instance Methods

banner(event) click to toggle source

Outputs test case header for given event (error, fail & etc)

Example:

PASS test: Test decription.  (0.15s 0:00:02:059)
colorize_count(str, count, colorize_method) click to toggle source

Creates an optionally-colorized string describing the number of occurances an event occurred.

@param [String] str A printf-style string that expects an integer argument (i.e. the count) @param [Integer] count The number of occurances of the event being described. @param [nil, Symbol] colorize_method The method on Colorize to call in order to apply color to the result, or nil

to not apply any coloring at all.
# File lib/turn/reporters/pretty_reporter.rb, line 114
def colorize_count(str, count, colorize_method)
  str= str % [count]
  str= Colorize.send(colorize_method, str) if colorize_method and count != 0
  str
end
prettify(raised, message=nil) click to toggle source

Cleanups and prints test payload

Example:

  fail is not 1
@ test/test_runners.rb:46:in `test_autorun_with_trace'
  bin/turn:4:in `<main>'
# File lib/turn/reporters/pretty_reporter.rb, line 147
def prettify(raised, message=nil)
  # Get message from raised, if not given
  message ||= raised.message

  backtrace = raised.respond_to?(:backtrace) ? raised.backtrace : raised.location

  # Filter and clean backtrace
  backtrace = clean_backtrace(backtrace)

  # Add trace mark to first line.
  backtrace.first.insert(0, TRACE_MARK)

  io.puts Colorize.bold(message.tabto(TAB_SIZE))
  io.puts backtrace.shift.tabto(TAB_SIZE - TRACE_MARK.length)
  io.puts backtrace.join("\n").tabto(TAB_SIZE)
  io.puts
end