How to run only one test in rspec

Mostly while developing its extremely convenient to be able to run just one particular describe block or it block.

Rspec lets you do that using a combination of focus: true as well as adding some configuration.

Open your spec_helper.rb file and add the following 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 simply use the focus: true parameter 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 merging your changes upstream to maintain the sanity and friendliness of your teammates.

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...