Skip to content

Instantly share code, notes, and snippets.

@foton
Last active April 26, 2016 12:05
Show Gist options
  • Save foton/141b9f73caccf13ccfcc to your computer and use it in GitHub Desktop.
Save foton/141b9f73caccf13ccfcc to your computer and use it in GitHub Desktop.
minitest reporter, which shows rake rerun commands for failed test at the end
require "minitest/reporters"
module Minitest
module Reporters
class RakeRerunReporter < Minitest::Reporters::DefaultReporter
def initialize(options = {})
@rerun_user_prefix=options.fetch(:rerun_prefix, "")
super
end
def report
super
puts
unless @fast_fail
#print rerun commands
failed_or_error_tests=(tests.select {|t| t.failure && !t.skipped? })
unless failed_or_error_tests.empty?
puts red("You can rerun failed/error test by commands (you can add rerun prefix with 'rerun_prefix' option):")
failed_or_error_tests.each do |test|
print_rerun_command(test)
end
end
end
#summary for all suite again
puts
print colored_for(suite_result, result_line)
puts
end
private
def print_rerun_command(test)
message = rerun_message_for(test)
unless message.nil? || message.strip == ''
puts
puts colored_for(result(test), message)
end
end
def rerun_message_for(test)
file_path=location(test.failure).gsub(/(\:\d*)\z/,"")
msg="#{@rerun_user_prefix} rake test TEST=#{file_path} TESTOPTS=\"--name=#{test.name} -v\""
if test.skipped?
"Skipped: \n#{msg}"
elsif test.error?
"Error:\n#{msg}"
else
"Failure:\n#{msg}"
end
end
def location(exception)
last_before_assertion = ''
exception.backtrace.reverse_each do |ss|
break if ss =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
last_before_assertion = ss
break if ss=~ /_test.rb\:/
end
last_before_assertion.sub(/:in .*$/, '')
end
end
end
end
#requires minitest reporters : https://github.com/kern/minitest-reporters
require "minitest/reporters"
require "rake_rerun_reporter"
reporter_options = { color: true, slow_count: 5, verbose: false, rerun_prefix: "rm -f log/test.log && bundle exec" }
Minitest::Reporters.use! [Minitest::Reporters::RakeRerunReporter.new(reporter_options)]
# test_helper.rb continues below
@foton
Copy link
Author

foton commented Mar 7, 2016

modified location method to correctly show test file error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment