RSpec::Retry

RSpec::Retry adds a :retry option for intermittently failing rspec examples. If an example has the :retry option, rspec will retry the example the specified number of times until the example succeeds.

Installation

Add this line to your application's Gemfile:

gem 'rspec-retry'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-retry

require in spec_helper.rb

# spec/spec_helper.rb
require 'rspec/retry'

RSpec.configure do |config|
  config.verbose_retry = true # show retry status in spec process
end

Usage

it 'should randomly succeed', :retry => 3 do
  expect(rand(2)).to eq(1)
end

it 'should succeed after a while', :retry => 3, :retry_wait => 10 do
  expect(command('service myservice status')).to eq('started')
end
# run spec (following log is shown if verbose_retry options is true)
# RSpec::Retry: 2nd try ./spec/lib/random_spec.rb:49
# RSpec::Retry: 3rd try ./spec/lib/random_spec.rb:49

Configuration

  • :verbose_retry(default: false) Print retry status
  • :default_retry_count(default: 1) If retry count is not set in an example, this value is used by default
  • :default_sleep_interval(default: 0) Seconds to wait between retries
  • :clear_lets_on_failure(default: true) Clear memoized values for lets before retrying
  • :exceptions_to_retry(default: []) List of exceptions that will trigger a retry (when empty, all exceptions will)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a pull request