How to run only one test in rspec

Mostly while devel­op­ing its extremely con­ve­nient to be able to run just one par­tic­u­lar describe block or it block.

Rspec lets you do that using a com­bi­na­tion of focus: true as well as adding some configuration.

Open your spec_helper.rb file and add the fol­low­ing line to your configuration

RSpec.configure do |config|
  .....some stuff....

  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  .....more of some stuff....

Once thats done, you can sim­ply use the focus: true para­me­ter in your tests as follows

# To run one describe block
describe MyCustomClass, focus: true do

# To run just one it block
it 'tests something meaningful', focus: true do
  # test something here

Now when you run your tests using rails spec, only the one that has focus: true will be executed.

Pro Tip: Just make sure to remove the focus: true before merg­ing your changes upstream to main­tain the san­ity and friend­li­ness of your teammates.

Ryan Sukale

Ryan is a UX engineer living in San Francisco, California.

You may also like...