Skip to content
Browse files

Combine DefaultReporter and SlowTestReporter.

  • Loading branch information...
1 parent b3065f2 commit a0eae83dfde4d3b88ccf8d7ca9a1372d838ffeed Alexander Kern committed
View
1 README.md
@@ -32,7 +32,6 @@ The following reporters are provided:
MiniTest::Reporters::RubyMateReporter # => Simple reporter designed for RubyMate
MiniTest::Reporters::RubyMineReporter # => Reporter designed for RubyMine IDE and TeamCity CI server; see below
MiniTest::Reporters::GuardReporter # => Integrates with guard-minitest to provide on-screen notifications
- MiniTest::Reporters::SlowTestReporter # => DefaultReporter with the addition of a list of your slowest tests
## Note on Patches/Pull Requests ##
View
3 Rakefile
@@ -23,8 +23,7 @@ task :gallery do
"JUnitReporter",
"ProgressReporter",
"RubyMateReporter",
- "SpecReporter",
- "SlowTestReporter"
+ "SpecReporter"
].each do |reporter|
puts "Running gallery tests using #{reporter}..."
sh "rake test:gallery REPORTER=#{reporter}" do
View
1 lib/minitest/reporters.rb
@@ -17,7 +17,6 @@ module Reporters
autoload :RubyMineReporter, "minitest/reporters/rubymine_reporter"
autoload :GuardReporter, "minitest/reporters/guard_reporter"
autoload :JUnitReporter, "minitest/reporters/junit_reporter"
- autoload :SlowTestReporter, "minitest/reporters/slow_test_reporter"
def self.use!(console_reporters = ProgressReporter.new, env = ENV, backtrace_filter = ExtensibleBacktraceFilter.default_filter)
use_runner!(console_reporters, env)
View
18 lib/minitest/reporters/default_reporter.rb
@@ -13,6 +13,8 @@ class DefaultReporter
def initialize(options = {})
@detailed_skip = options.fetch(:detailed_skip, true)
+ @slow_count = options.fetch(:slow_count, 0)
+ @test_times = []
@color = options.fetch(:color) do
output.tty? && (
ENV["TERM"] == "screen" ||
@@ -29,7 +31,8 @@ def before_suites(suites, type)
end
def before_test(suite, test)
- print "#{suite}##{test} = " if verbose?
+ @test_name = "#{suite}##{test}"
+ print "#{@test_name} = " if verbose?
end
def pass(suite, test, test_runner)
@@ -66,6 +69,18 @@ def after_suites(suites, type)
end
end
+ if @slow_count > 0
+ slow_tests = @test_times.sort_by { |x| x[1] }.reverse.take(@slow_count)
+
+ puts
+ puts "Slowest tests:"
+ puts
+
+ slow_tests.each do |slow_test|
+ puts "%.6fs %s" % [slow_test[1], slow_test[0]]
+ end
+ end
+
puts
puts colored_for(suite_result, result_line)
end
@@ -103,6 +118,7 @@ def suite_result
def after_test(result)
time = Time.now - runner.test_start_time
+ @test_times << [@test_name, time]
print '%.2f s = ' % time if verbose?
print result
View
154 lib/minitest/reporters/slow_test_reporter.rb
@@ -1,154 +0,0 @@
-require 'ansi/code'
-
-module MiniTest
- module Reporters
- # A reporter identical to the DefaultReporter, but that also
- # prints out a list of the slowest tests in the test run
- # so that you know where to focus optimization efforts.
- class SlowTestReporter
- include Reporter
-
- def initialize(options = {})
- @detailed_skip = options.fetch(:detailed_skip, true)
- @color = options.fetch(:color) do
- output.tty? && (
- ENV["TERM"] == "screen" ||
- ENV["TERM"] =~ /term(?:-(?:256)?color)?\z/ ||
- ENV["EMACS"] == "t"
- )
- end
- @slow_count = options.fetch(:slow_count, 10)
- @results = {}
- end
-
- def before_suites(suites, type)
- puts
- puts "# Running #{type}s:"
- puts
- end
-
- def before_test(suite, test)
- print "#{suite}##{test} = " if verbose?
- @decorated_name = "#{suite}##{test}"
- end
-
- def pass(suite, test, test_runner)
- after_test(green('.'))
- end
-
- def skip(suite, test, test_runner)
- after_test(yellow('S'))
- end
-
- def failure(suite, test, test_runner)
- after_test(red('F'))
- end
-
- def error(suite, test, test_runner)
- after_test(red('E'))
- end
-
- def after_suites(suites, type)
- time = Time.now - runner.suites_start_time
- status_line = "Finished %ss in %.6fs, %.4f tests/s, %.4f assertions/s." %
- [type, time, runner.test_count / time, runner.assertion_count / time]
-
- puts
- puts
- puts colored_for(suite_result, status_line)
-
- runner.test_results.each do |suite, tests|
- tests.each do |test, test_runner|
- if message = message_for(test_runner)
- puts
- print colored_for(test_runner.result, message)
- end
- end
- end
-
- puts
- puts colored_for(suite_result, result_line)
-
- slow_tests = @results.sort{|x,y| y[1]<=>x[1]}[0, @slow_count]
- slow_tests.each do |slow_test|
- print '%.6fs %s' % [slow_test[1], slow_test[0]]
- puts
- end
- end
-
- private
-
- def green(string)
- @color ? ANSI::Code.green(string) : string
- end
-
- def yellow(string)
- @color ? ANSI::Code.yellow(string) : string
- end
-
- def red(string)
- @color ? ANSI::Code.red(string) : string
- end
-
- def colored_for(result, string)
- case result
- when :failure, :error; red(string)
- when :skip; yellow(string)
- else green(string)
- end
- end
-
- def suite_result
- case
- when runner.failures > 0; :failure
- when runner.errors > 0; :error
- when runner.skips > 0; :skip
- else :pass
- end
- end
-
- def after_test(result)
- time = Time.now - runner.test_start_time
- @results[@decorated_name] = time
-
- print '%.2f s = ' % time if verbose?
- print result
- puts if verbose?
- end
-
- def location(exception)
- last_before_assertion = ''
-
- exception.backtrace.reverse_each do |s|
- break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
- last_before_assertion = s
- end
-
- last_before_assertion.sub(/:in .*$/, '')
- end
-
- def message_for(test_runner)
- suite = test_runner.suite
- test = test_runner.test
- e = test_runner.exception
-
- case test_runner.result
- when :pass then nil
- when :skip
- if @detailed_skip
- "Skipped:\n#{test}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
- end
- when :failure then "Failure:\n#{test}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
- when :error
- bt = filter_backtrace(test_runner.exception.backtrace).join "\n "
- "Error:\n#{test}(#{suite}):\n#{e.class}: #{e.message}\n #{bt}\n"
- end
- end
-
- def result_line
- '%d tests, %d assertions, %d failures, %d errors, %d skips' %
- [runner.test_count, runner.assertion_count, runner.failures, runner.errors, runner.skips]
- end
- end
- end
-end
View
2 test/test_helper.rb
@@ -15,5 +15,5 @@ class TestCase < MiniTest::Unit::TestCase
reporter_klass = MiniTest::Reporters.const_get(ENV["REPORTER"])
MiniTest::Reporters.use! [reporter_klass.new]
else
- MiniTest::Reporters.use!
+ MiniTest::Reporters.use!(MiniTest::Reporters::DefaultReporter.new)
end

0 comments on commit a0eae83

Please sign in to comment.
Something went wrong with that request. Please try again.