Repeats an RSpec example until it succeeds
# spec_helper.rb# Example: Repeat all tests in CIifENV['CI']require'rspec/repeat'RSpec.configuredo |config|
config.includeRSpec::Repeatconfig.around:eachdo |example|
repeatexample,3.times,verbose: trueendendendrepeat example, 3.times, { options }
You can pass an options hash:
- clear_let(Boolean) — if false,
letdeclarations 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.rbrequire'rspec/repeat'RSpec.configuredo |config|
config.includeRSpec::Repeatconfig.around:each,:stubborndo |example|
repeatexample,3.timesendenddescribe'stubborn tests',:stubborndo# ...endThis will make all spec/features/ retry thrice. Perfect for Poltergeist/Selenium tests that intermittently fail for no reason.
# rails_helper.rb or spec_helper.rbrequire'rspec/repeat'RSpec.configuredo |config|
config.includeRSpec::Repeatconfig.around:each,type: :featuredo |example|
repeatexample,3.timesendendIn these cases, it'd be smart to restrict which exceptions to be retried.
repeatexample,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'doincludeRSpec::Repeatarounddo |example|
repeatexample,10.timesendit'works, eventually'doexpect(rand(2)).toeq0endendMuch 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