| 1 |
module Minitest |
| 2 |
module Reporters |
| 3 |
# Turn-like reporter that reads like a spec. |
| 4 |
# |
| 5 |
# Based upon TwP's turn (MIT License) and paydro's monkey-patch. |
| 6 |
# |
| 7 |
# @see https://github.com/TwP/turn turn |
| 8 |
# @see https://gist.github.com/356945 paydro's monkey-patch |
| 9 |
class SpecReporter < BaseReporter |
| 10 |
include ANSI::Code |
| 11 |
include RelativePosition |
| 12 |
|
| 13 |
# The constructor takes an `options` hash |
| 14 |
# @param options [Hash] |
| 15 |
# @option options print_failure_summary [Boolean] whether to print the errors at the bottom of the |
| 16 |
# report. |
| 17 |
# @option options suppress_inline_failure_output [Boolean] whether to suppress the printing of errors |
| 18 |
# inline with the test results as they occur. |
| 19 |
# |
| 20 |
def initialize(options = {}) |
| 21 |
super |
| 22 |
@print_failure_summary = options[:print_failure_summary] |
| 23 |
@suppress_inline_failure_output = options[:suppress_inline_failure_output] |
| 24 |
end |
| 25 |
|
| 26 |
def start |
| 27 |
super |
| 28 |
puts('Started with run options %s' % options[:args]) |
| 29 |
puts |
| 30 |
end |
| 31 |
|
| 32 |
def report |
| 33 |
super |
| 34 |
if @print_failure_summary |
| 35 |
failed_test_groups = tests.reject { |test| test.failures.empty? } |
| 36 |
.sort_by { |test| [test_class(test).to_s, test.name] } |
| 37 |
.group_by { |test| test_class(test).to_s } |
| 38 |
unless failed_test_groups.empty? |
| 39 |
print(red('Failures and errors:')) |
| 40 |
|
| 41 |
failed_test_groups.each { |name, tests| print_failure(name, tests) } |
| 42 |
end |
| 43 |
end |
| 44 |
|
| 45 |
puts('Finished in %.5fs' % total_time) |
| 46 |
print('%d tests, %d assertions, ' % [count, assertions]) |
| 47 |
color = failures.zero? && errors.zero? ? :green : :red |
| 48 |
print(send(color) { '%d failures, %d errors, ' } % [failures, errors]) |
| 49 |
print(yellow { '%d skips' } % skips) |
| 50 |
puts |
| 51 |
end |
| 52 |
|
| 53 |
def record(test) |
| 54 |
super |
| 55 |
record_print_status(test) |
| 56 |
record_print_failures_if_any(test) unless @suppress_inline_failure_output |
| 57 |
end |
| 58 |
|
| 59 |
protected |
| 60 |
|
| 61 |
def before_suite(suite) |
| 62 |
puts suite |
| 63 |
end |
| 64 |
|
| 65 |
def after_suite(_suite) |
| 66 |
puts |
| 67 |
end |
| 68 |
|
| 69 |
def print_failure(name, tests) |
| 70 |
puts |
| 71 |
puts name |
| 72 |
tests.each do |test| |
| 73 |
record_print_status(test) |
| 74 |
print_info(test.failure, test.error?) |
| 75 |
puts |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
def record_print_failures_if_any(test) |
| 80 |
if !test.skipped? && test.failure |
| 81 |
print_info(test.failure, test.error?) |
| 82 |
puts |
| 83 |
end |
| 84 |
end |
| 85 |
|
| 86 |
def record_print_status(test) |
| 87 |
test_name = test.name.gsub(/^test_: /, 'test:') |
| 88 |
print pad_test(test_name) |
| 89 |
print_colored_status(test) |
| 90 |
print(" (%.2fs)" % test.time) unless test.time.nil? |
| 91 |
puts |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
end |