Need help with rspec-retry?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

517 Stars 87 Forks MIT License 164 Commits 30 Opened issues


retry randomly failing rspec example

Services available


Need anything else?

Contributors list

RSpec::Retry Build Status

RSpec::Retry adds a

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


| Rspec Version | Rspec-Retry Version | |---------------|---------------------| | > 3.8 | 0.6.2 but untested | | > 3.3, <= 3.8 | 0.6.2             | | 3.2       | 0.4.6             | | 2.14.8       | 0.4.4             |


Add this line to your application's Gemfile:

gem 'rspec-retry', group: :test # Unlike rspec, this doesn't need to be included in development group

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-retry

require in

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

RSpec.configure do |config|

show retry status in spec process

config.verbose_retry = true

show exception that triggers a retry if verbose_retry is set to true

config.display_try_failure_messages = true

run retry only on features

config.around :each, :js do |ex| ex.run_with_retry retry: 3 end

callback to be run between retries

config.retry_callback = proc do |ex| # run some additional clean up task - can be filtered by example metadata if ex.metadata[:js] Capybara.reset! end end end


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

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


You can call

on an individual example.


  • :verbose_retry(default: false) Print retry status
  • :displaytryfailure_messages (default: false) If verbose retry is enabled, print what reason forced the retry
  • :defaultretrycount(default: 1) If retry count is not set in an example, this value is used by default. Note that currently this is a 'try' count. If increased from the default of 1, all examples will be retried. We plan to fix this as a breaking change in version 1.0.
  • :defaultsleepinterval(default: 0) Seconds to wait between retries
  • :clearletson_failure(default: true) Clear memoized values for
    s before retrying
  • :exceptionstohard_fail(default: []) List of exceptions that will trigger an immediate test failure without retry. Takes precedence over :exceptionstoretry
  • :exceptionstoretry(default: []) List of exceptions that will trigger a retry (when empty, all exceptions will)
  • :retry_callback(default: nil) Callback function to be called between retries

Environment Variables

  • RSPECRETRYRETRY_COUNT can override the retry counts even if a retry count is set in an example or defaultretrycount is set in a configuration.


  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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.