RSpec: How to retry examples

Posted . Visible to the public.

In some projects we have issues with flaky tests. The best default is to fix them all. But in some cases it might be a pragmatic way to retry them for a limit number of times.

Notes:

Gemfile.lock

group :test do
  gem 'rspec'
  gem 'rspec-rebound'
end

spec/spec_helper.rb

require 'rspec/rebound'

RSpec.configure do |config|
  # https://github.com/windmotion-io/rspec-rebound/blob/master/README.md
  if ENV['CI']
    config.default_retry_count = 3
    config.verbose_retry = true
    config.display_try_failure_messages = true
  end
end

Example Screenshots

Here is an example output for a flaky test that passed the second time:

Image

Here is an example output for a flaky test that retries to often (the example set default_retry_count to 1):

Image

Last edit
Emanuel
Keywords
flaky, test, rspec, retry
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2026-02-13 16:18)