Created
April 29, 2015 15:43
-
-
Save johnathanludwig/c3bde185d4e933334dbf to your computer and use it in GitHub Desktop.
This is a custom reporter used with minitest-reporters. Error messages may get cut off. Still trying to find the best way to print them without printing the stack trace twice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ansi/code' | |
module Minitest | |
module Reporters | |
# Features: | |
# Like the SpecReporter but with the pass/time on the left | |
# Collects the message before doing a puts so that lines dont get mixed up when running tests in threads | |
# Highlights lines in a stacktrace from this project | |
# Highlights slow tests | |
class ParallelReporter < BaseReporter | |
# fix coloring for parallel_tests | |
require 'ansi/code' | |
include ::ANSI::Code | |
extend ::ANSI::Code | |
include RelativePosition | |
def report | |
super | |
msg = "\n" | |
msg += "Finished in #{format('%f', total_time)}s" | |
msg += "\n" | |
msg += "#{format('%d', count)} tests, #{format('%d', assertions)} assertions, " | |
color = failures.zero? && errors.zero? ? :green : :red | |
msg += send(color) { "#{format('%d', failures)} failures, #{format('%d', errors)} errors, " } | |
msg += yellow { "#{format('%d', skips)} skips" } | |
msg += "\n" | |
puts msg | |
end | |
def record(test) | |
super | |
msg = '' | |
msg += colored_status(test) | |
msg += color_by_time(" (#{format('%.2f', test.time)}s) ", test.time) | |
msg += pad_test(test.name).gsub('test_: ', '') | |
msg += stacktrace(test) | |
puts msg | |
end | |
private | |
def color_by_time(string, time) | |
if time > 5 | |
red string | |
elsif time > 1 | |
yellow string | |
else | |
string | |
end | |
end | |
def colored_status(test) | |
if test.passed? | |
green { pad_mark(result(test).to_s.upcase) } | |
elsif test.skipped? | |
yellow { pad_mark(result(test).to_s.upcase) } | |
else | |
red { pad_mark(result(test).to_s.upcase) } | |
end | |
end | |
def stacktrace(test) # rubocop:disable Metrics/MethodLength | |
return '' unless !test.skipped? && test.failure | |
msg = "\n" | |
msg += red(test.failure.message.split("\n ").first.to_s) + "\n" | |
test.failure.backtrace.each do |line| | |
if line.include?(Rails.root.to_s) | |
msg += yellow line | |
else | |
msg += line | |
end | |
msg += "\n" | |
end | |
msg | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment