Skip to content

Instantly share code, notes, and snippets.

@naps62
Created May 30, 2014 13:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naps62/b6e405e3157b5a4d29e2 to your computer and use it in GitHub Desktop.
Save naps62/b6e405e3157b5a4d29e2 to your computer and use it in GitHub Desktop.
Retrying failing cucumber steps automatically until everything is green
<% std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip -r features/support -r features/step_definitions" %>
<% rerun_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --format rerun --out tmp/rerun.txt --strict --tags ~@wip -r features/support -r features/step_definitions" %>
default: <%= std_opts %> features
all: <%= rerun_opts %> features
rerun: <%= rerun_opts %> @tmp/rerun.txt
require 'cucumber/rake/task'
NUM_RETRIES = ENV['NUM_RETRIES'] || 2
task :default => "cucumber:run"
namespace :cucumber do
directory "tmp"
@rerun_file = 'tmp/rerun.txt'
Cucumber::Rake::Task.new(:all) do |task|
task.profile = "all"
end
desc 'Run cucumber features'
task :run => "tmp" do
retry_on_failure do
run_features
end
clean_up
exit @exit_status
end
def retry_on_failure
rm_rf @rerun_file
@retries = 0
begin
@exit_status = 0
yield
rescue SystemExit => e
@exit_status = e.status
if retry?(exception: e)
@retries += 1
retry
end
end
end
def run_features
if File.exists? @rerun_file
Cucumber::Rake::Task::ForkedCucumberRunner.new(['lib'], Cucumber::BINARY, ['--profile', 'rerun'], true, []).run
else
Rake::Task["cucumber:all"].invoke
end
end
def retry?(exception: nil)
@retries < NUM_RETRIES && !exception.success?
end
def clean_up
rm_rf @rerun_file.pathmap("%d")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment