Rspec::Repeat

Repeats an RSpec example until it succeeds.

describe 'a stubborn test' do
  include Rspec::Repeat

  around do |example|
    repeat example, 10.times
  end

  it 'works, eventually' do
    expect(rand(2)).to eq 0
  end
end

Status


Advanced usage

Options

repeat example, 3.times, { options }

You can pass an options hash:

  • clear_let (Boolean) - if false, let declarations will not be cleared.
  • exceptions (Array) - if given, it will only retry exception classes from this list.
  • wait (Numeric) - seconds to wait between each retry.
  • verbose (Boolean) - if true, it will print messages upon failure.

Attaching to tags

This will allow you to repeat any example multiple times by tagging it.

# rails_helper.rb or spec_helper.rb
RSpec.configure do
  config.around :each, :foobar do |example|
    repeat example, 3.times
  end
end
describe 'stubborn tests', :foobar do
  # ...
end

Attaching to features

This will make all spec/features/ retry thrice. Perfect for Poltergeist/Selenium tests that intermittently fail for no reason.

# rails_helper.rb or spec_helper.rb
RSpec.configure do
  config.around :each, type: :feature do
    repeat example, 3.times
  end
end

In these cases, it'd be smart to restrict which exceptions to be retried.

repeat example, 3.times, exceptions: [ Net::ReadTimeout ]


Acknowledgement

Much of this code has been refactored out of rspec-retry by @NoRedInk.


Thanks

rspec-repeat © 2015+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz