Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Emulates the QuickCheck ?SOMETIMES macro.
module Sometimes
def run_with_retries(example_to_run, retries)
self.example.metadata[:retries] ||= retries
retries.times do |t|
self.example.metadata[:retried] = t + 1
self.example.instance_variable_set(:@exception, nil)
example_to_run.run
break unless self.example.exception
end
if e = self.example.exception
new_exception = e.exception(e.message + "[Retried #{retries} times]")
new_exception.set_backtrace e.backtrace
self.example.instance_variable_set(:@exception, new_exception)
end
end
end
RSpec.configure do |config|
config.include Sometimes
config.alias_example_to :sometimes, :sometimes => true
config.add_setting :sometimes_retry_count, :default => 3
config.around(:each, :sometimes => true) do |example|
retries = example.metadata[:retries] || RSpec.configuration.sometimes_retry_count
run_with_retries(example, retries)
end
config.after(:suite) do
formatter = RSpec.configuration.formatters.first
color = lambda {|tint, msg| formatter.send(tint, msg) }
retried_examples = RSpec.world.example_groups.map do |g|
g.descendants.map do |d|
d.filtered_examples.select {|e| e.metadata[:sometimes] && e.metadata[:retried] > 1 }
end
end.flatten
formatter.message color[retried_examples.empty? ? :green : :yellow, "\n\nRetried examples: #{retried_examples.count}"]
unless retried_examples.empty?
retried_examples.each do |e|
formatter.message " #{e.full_description}"
formatter.message(color[:yellow, " [#{e.metadata[:retried]}/#{e.metadata[:retries]}] "] + formatter.class.relative_path(e.location))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.