Read more

RSpec: run a single spec (Example or ExampleGroup)

Daniel Straßner
September 28, 2017Software engineer at makandra GmbH

RSpec allows you to mark a single Example/ExampleGroup so that only this will be run. This is very useful when using a test runner like guard.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Add the following config to spec/spec_helper.rb:

RSpec.configure do |config|
  # These two settings work together to allow you to limit a spec run
  # to individual examples or groups you care about by tagging them with
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
  # get run.
  config.filter_run_including :focus => true
  config.run_all_when_everything_filtered = true
  ...
  
end

Now you can mark a single Example/ExampleGroup with the "focus"-tag so only the tagged spec will be run:

describe SomeClass do
  it 'does great stuff', :focus do
    ... 
  end
  ...
end

Alternatively you can prepend f to any of it, describe and context which will result in fit, fdescribe and fcontext:

describe SomeClass do
  fit 'does great stuff' do
    ... 
  end
  ...
end

Analogously you can exclude Examples or Example Groups from being run by prepending x (xit, xdescribe and xcontext). Good thing is that those will be shown as pending in the RSpec output, so you won't be tempted to commit excluded Examples to source control.

Note

In newer versions of rspec this can be achieved by config.filter_run_when_matching(:focus)

Posted by Daniel Straßner to makandra dev (2017-09-28 15:22)