RSpec: Efficiently rerunning failed examples during development

Note

Don't use reruns as a mean to work around flaky tests. You should always try to fix those instead of rerunning them regularly.

Setup

Configure RSpec to persist the result of your test runs to a file. This is necessary to be able to rerun examples.
Add this to your spec/spec_helper.rb :

  config.example_status_persistence_file_path = 'spec/examples.txt'

Rerun all failed examples using --only-failures

bundle exec rspec --only-failures (or g rs --only-failures with geordi) will rerun all examples which failed in the last run.

Rerun a single failed example using --next-failure

bundle exec rspec --next-failure (or g rs --next-failure with geordi) will rerun only the first previously failed example.
Once you fixed this spec and it´s green again the next run of g rs --next-failure will run the next failed example. This will continue until all specs are green and can be very useful if you just implemented your new feature and now need to fix all the specs it broke.

Martin Schaflitzl 7 months ago