Module: TopTests::ClassMethods

Defined in:
lib/top_tests.rb

Overview

Class methods ###

Instance Method Summary collapse

Instance Method Details

#after_all_testsObject



84
85
86
87
# File 'lib/top_tests.rb', line 84

def after_all_tests
  check_tests_duration
  print_top_tests
end

#check_tests_durationObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/top_tests.rb', line 71

def check_tests_duration
  if !slow_tests.empty?
    if slow_tests.size == 1
      puts "\nTEST?FAIL! 1 test is taking longer than #{max_duration} seconds:"
    else
      puts "\nTEST?FAIL! #{slow_tests.count} tests are taking longer than #{max_duration} seconds:"
    end
    puts format_tests(slow_tests)
    puts
    exit 1
  end
end

#format_tests(tests) ⇒ Object



61
62
63
# File 'lib/top_tests.rb', line 61

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

#max_durationObject



49
50
51
# File 'lib/top_tests.rb', line 49

def max_duration
  @@max_duration ||= nil
end

#max_duration=(seconds) ⇒ Object



45
46
47
# File 'lib/top_tests.rb', line 45

def max_duration=(seconds)
  @@max_duration = seconds
end


65
66
67
68
69
# File 'lib/top_tests.rb', line 65

def print_top_tests
  puts "\nTop 10 slowest tests:"
  puts format_tests(top_tests.shift(10))
  puts
end

#slow_testsObject



57
58
59
# File 'lib/top_tests.rb', line 57

def slow_tests
  max_duration ? top_tests.find_all { |t| t.last > max_duration } : []
end

#tests_durationsObject



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

def tests_durations
  @@tests_durations ||= []
end

#top_testsObject



53
54
55
# File 'lib/top_tests.rb', line 53

def top_tests
  tests_durations.sort { |a, b| b.last <=> a.last }
end