module Protest::Utils::Summaries
Mixin that provides summaries for your text based test runs.
Public Instance Methods
Call on :end
to print a list of failures (failed assertions) and errors (unrescued exceptions), including file and line number where the test failed, and a backtrace.
It will not output anything if there weren't any failures or errors.
For example:
on :end do |report| report.puts report.summarize_errors end
This relies on the public Report
API, and on the presence of a puts method to write to whatever source you are writing your report.
# File lib/protest/utils/summaries.rb, line 65 def summarize_errors return if failures_and_errors.empty? puts "Failures:" puts pad_indexes = failures_and_errors.size.to_s.size failures_and_errors.each_with_index do |error, index| if ErroredTest === error colorize_as = :errored error_prefix = "With #{error.error.class.name}:" else colorize_as = :failed error_prefix = "With" end lines = [] lines << " #{pad(index+1, pad_indexes)}) #{test_type(error)}: `#{error.test_name}' (on line #{error.line} of `#{error.file}')" lines.concat indent("#{error_prefix} `#{error.error_message}'", 6 + pad_indexes) lines.concat indent(error.backtrace, 6 + pad_indexes) lines.each { |line| puts line, colorize_as } puts end end
Call on :end
to print a list of pending tests, including file and line number where the call to TestCase#pending
+ was made.
It will not output anything if there weren't any pending tests.
For example:
on :end do |report| report.puts report.summarize_pending_tests end
This relies on the public Report
API, and on the presence of a puts method to write to whatever source you are writing your report.
# File lib/protest/utils/summaries.rb, line 36 def summarize_pending_tests return if pendings.empty? puts "Pending tests:" puts pad_indexes = pendings.size.to_s.size pendings.each_with_index do |pending, index| puts " #{pad(index+1, pad_indexes)}) #{pending.test_name} (#{pending.pending_message})", :pending puts indent("On line #{pending.line} of `#{pending.file}'", 6 + pad_indexes), :pending puts end end
Call on :end
to output the amount of tests (passed, pending, failed and errored), the amount of assertions, and the time elapsed.
For example:
on :end do |report| report.puts report.summarize_test_totals end
This relies on the public Report
API, and on the presence of a puts method to write to whatever source you are writing your report.
# File lib/protest/utils/summaries.rb, line 17 def summarize_test_totals puts test_totals puts running_time end
Private Instance Methods
# File lib/protest/utils/summaries.rb, line 107 def indent(strings, size=2, indent_with=" ") Array(strings).map do |str| str.to_s.split("\n").map {|s| indent_with * size + s }.join("\n") end end
# File lib/protest/utils/summaries.rb, line 113 def pad(str, amount) " " * (amount - str.to_s.size) + str.to_s end
# File lib/protest/utils/summaries.rb, line 92 def running_time "Ran in #{time_elapsed} seconds" end
# File lib/protest/utils/summaries.rb, line 96 def test_totals "%d test%s, %d assertion%s (%d passed, %d pending, %d failed, %d errored)" % [total_tests, total_tests == 1 ? "" : "s", assertions, assertions == 1 ? "" : "s", passes.size, pendings.size, failures.size, errors.size] end
# File lib/protest/utils/summaries.rb, line 117 def test_type(test) case test when ErroredTest; "Error" when FailedTest; "Failure" end end