RSpec: Efficiently rerunning failed examples during development

Updated . Posted . Visible to the public.

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
Last edit
Julian
License
Source code in this card is licensed under the MIT License.
Posted by Martin Schaflitzl to makandra dev (2023-10-09 08:04)