Class: Minitest::TopTestReporter

Inherits:
AbstractReporter
  • Object
show all
Defined in:
lib/minitest/top_tests_plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TopTestReporter

Returns a new instance of TopTestReporter.



15
16
17
18
# File 'lib/minitest/top_tests_plugin.rb', line 15

def initialize(options)
  @results = []
  @max_time = options[:max_time]
end

Instance Attribute Details

#max_timeObject (readonly)

Returns the value of attribute max_time.



13
14
15
# File 'lib/minitest/top_tests_plugin.rb', line 13

def max_time
  @max_time
end

#resultsObject (readonly)

Returns the value of attribute results.



13
14
15
# File 'lib/minitest/top_tests_plugin.rb', line 13

def results
  @results
end

Instance Method Details

#format_tests(tests) ⇒ Object



56
57
58
# File 'lib/minitest/top_tests_plugin.rb', line 56

def format_tests(tests)
  tests.map { |t| "  #{format("%7.3f", t.time)} #{t.klass}##{t.name}" }.join("\n")
end

#passed?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/minitest/top_tests_plugin.rb', line 25

def passed?
  slow_tests.empty?
end


45
46
47
48
49
50
51
52
53
54
# File 'lib/minitest/top_tests_plugin.rb', line 45

def print_slow_tests
  if !slow_tests.empty?
    if slow_tests.size == 1
      puts "\n1 test is taking longer than #{max_time} seconds:"
    else
      puts "\n3 tests are taking longer than #{max_time} seconds:"
    end
    puts format_tests(slow_tests)
  end
end

#record(result) ⇒ Object



20
21
22
23
# File 'lib/minitest/top_tests_plugin.rb', line 20

def record(result)
  results << result
  #result.refute(true, "Too long") if max_time && result.time > max_time
end

#reportObject



29
30
31
32
33
34
35
# File 'lib/minitest/top_tests_plugin.rb', line 29

def report
  if slow_tests.empty?
    puts "\nTop 10 slowest tests:\n#{format_tests(slowest_results[0,10])}"
  else
    print_slow_tests
  end
end

#slow_testsObject



41
42
43
# File 'lib/minitest/top_tests_plugin.rb', line 41

def slow_tests
  max_time ? results.find_all { |r| r.time > max_time } : []
end

#slowest_resultsObject



37
38
39
# File 'lib/minitest/top_tests_plugin.rb', line 37

def slowest_results
  results.sort { |a, b| b.time <=> a.time }
end