Jeff Kreeftmeijer

Making RSpec stop operation immediately after failing

2010-11-08

Imagine you’re running your whole RSpec spec suite and the first or second spec fails. You have about two hundred specs to go, but you want to dive in and fix that first failure right away. You’d probably interrupt the run to get the failure details to go fix them, right?

While this works most of the time (you might get some problems interrupting the run when using Selenium) wouldn’t it be awesome if RSpec could automatically stop and output when running into the first failure?

Aside from a three year old thread in the rspec-users group, I couldn’t find a lot of useful data. So I set out to write something myself.

Fail-fast

I started poking around in the RSpec source and accidentally stumbled on RSpec’s generated documentation on Relish, where I found the fail_fast configuration option that got introduced in rspec-core 2.0.0.rc. It actually does exactly what I wanted to achieve.

The fail_fast option will cause RSpec to immediately stop running your specs and output the failure details whenever it fails for the first time, giving you a nice speed boost when trying to fix some spec fails. Usage is pretty straightforward:

# spec/spec_helper.rb

RSpec.configure do |c|
  c.fail_fast = true
end

Command line support

Since you probably still want to run your whole spec suite without using the fail_fast option from time to time, setting it in spec/spec_helper.rb isn’t really a great option. So I added a command line option so you can use it whenever you want:

$ bundle exec rspec spec/ --fail-fast
.F

Failures:
  1) Swinger should set the Capybara driver
     Failure/Error: Capybara.current_driver.should_not == :rack_test

Finished in 0.00479 seconds
2 examples, 1 failure

The --fail-fast option was only released yesterday as a part of RSpec 2.1, so don’t forget to update.