Skip to content

Repository files navigation

RSpec::Repeat

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: trueendendend

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.rbrequire'rspec/repeat'RSpec.configuredo |config|
config.includeRSpec::Repeatconfig.around:each,:stubborndo |example|
repeatexample,3.timesendend
describe'stubborn tests',:stubborndo# ...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.rbrequire'rspec/repeat'RSpec.configuredo |config|
config.includeRSpec::Repeatconfig.around:each,type: :featuredo |example|
repeatexample,3.timesendend

In 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]

Repeating a specific test

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)).toeq0endend

Acknowledgement

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


Thanks

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

About

Repeats an RSpec example until it succeeds.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages