Created
May 15, 2012 22:35
-
-
Save marcusbaguley/2705653 to your computer and use it in GitHub Desktop.
Slow running specs - just ignore them hack
This file contains 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
# Allow you to record slow specs and ignore them when running test suite | |
# 1. RECORD: slow_running_record=true rake spec | |
# 2. IGNORE: slow_running_ignore=true rake spec | |
# Don't ignore slow specs on CI server. Review the .slow_running-specs file | |
class SlowRunning | |
SLOW_TEST = ENV['slow_running_threshold'] || '1.5' | |
SLOW_RUNNING_SPEC_FILE = File.join(Rails.root, '.slow_running-specs') | |
cattr_accessor :file_created | |
cattr_accessor :slow_tests | |
def self.slow_tests | |
if @@slow_tests.nil? | |
@@slow_tests = [] | |
File.exists?(SLOW_RUNNING_SPEC_FILE) && CSV.foreach(SLOW_RUNNING_SPEC_FILE) do |row| | |
@@slow_tests << row.first | |
end | |
end | |
@@slow_tests | |
end | |
def self.run_unless_slow(name, &block) | |
if slow_tests.include?(name) | |
puts "Ignoring slow test: #{name}" | |
else | |
yield | |
end | |
end | |
def self.record(test_name, &block) | |
start = Time.now | |
yield | |
run_time = Time.now - start | |
if run_time > SLOW_TEST.to_f | |
puts "Adding slow test #{run_time}s: #{test_name}" | |
CSV.open(SLOW_RUNNING_SPEC_FILE, file_created ? 'ab' : 'wb') do |csv| | |
csv << [test_name, run_time] | |
end | |
self.file_created ||= true | |
end | |
end | |
end | |
if ENV['slow_running_ignore'] | |
RSpec.configure do |config| | |
config.around(:each) do |example| | |
SlowRunning.run_unless_slow(spec_name_from_example(example)) do | |
example.run | |
end | |
end | |
end | |
end | |
if ENV['slow_running_record'] | |
RSpec.configure do |config| | |
config.around(:each) do |example| | |
spec_name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_") | |
SlowRunning.record(spec_name) do | |
example.run | |
end | |
end | |
end | |
end | |
def spec_name_from_example(example) | |
example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment