Giter VIP home page Giter VIP logo

rspec-repeat's Introduction

RSpec::Repeat

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

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

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

Repeating a specific test

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

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

rspec-repeat's People

Contributors

rstacruz avatar nruth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.