Repeats an RSpec example until it succeeds
# spec_helper.rb
# Example: Repeat all tests in CI
if ENV['CI']
require 'rspec/repeat'
RSpec.configure do |config|
config.include RSpec::Repeat
config.around :each do |example|
repeat example, 3.times, verbose: true
end
end
end
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.
This will allow you to repeat any example multiple times by tagging it.
# rails_helper.rb or spec_helper.rb
require 'rspec/repeat'
RSpec.configure do |config|
config.include RSpec::Repeat
config.around :each, :stubborn do |example|
repeat example, 3.times
end
end
describe 'stubborn tests', :stubborn do
# ...
end
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
require 'rspec/repeat'
RSpec.configure do |config|
config.include RSpec::Repeat
config.around :each, type: :feature do |example|
repeat example, 3.times
end
end
In these cases, it'd be smart to restrict which exceptions to be retried.
repeat example, 3.times, verbose: true, exceptions: [
Net::ReadTimeout,
Selenium::WebDriver::Error::WebDriverError
]
You can also include RSpec::Repeat in just a single test block.
require 'rspec/repeat'
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
Much of this code has been refactored out of rspec-retry by @NoRedInk.
rspec-repeat © 2015-2017, 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