How do I run only specific tests in Rspec?

RubyRspec

Ruby Problem Overview


I think there's a way to run only tests with a given label. Anybody know?

Ruby Solutions


Solution 1 - Ruby

It isn't easy to find the documentation, but you can tag examples with a hash. Eg.

# spec/my_spec.rb
describe SomeContext do
  it "won't run this" do
    raise "never reached"
  end

  it "will run this", :focus => true do
    1.should == 1
  end
end

$ rspec --tag focus spec/my_spec.rb

More info on GitHub. (anyone with a better link, please advise)

(update)

RSpec is now superbly documented here. See the --tag option section for details.

As of v2.6 this kind of tag can be expressed even more simply by including the configuration option treat_symbols_as_metadata_keys_with_true_values, which allows you to do:

describe "Awesome feature", :awesome do

where :awesome is treated as if it were :awesome => true.

Also see this answer for how to configure RSpec to automatically run 'focused' tests. This works especially well with Guard.

Solution 2 - Ruby

You can run all tests that contain a specific string with --example (or -e) option:

rspec spec/models/user_spec.rb -e "User is admin"

I use that one the most.

Solution 3 - Ruby

Make sure RSpec is configured in your spec_helper.rb to pay attention to focus:

RSpec.configure do |config|
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true
end

Then in your specs, add focus: true as an argument:

it 'can do so and so', focus: true do
  # This is the only test that will run
end

You can also focus tests by changing it to fit (or exclude tests with xit), like so:

fit 'can do so and so' do
  # This is the only test that will run
end

Solution 4 - Ruby

alternatively you can pass the line number: rspec spec/my_spec.rb:75 - the line number can point to a single spec or a context/describe block (running all specs in that block)

Solution 5 - Ruby

You can also string multiple line numbers together with colon :

$ rspec ./spec/models/company_spec.rb:81:82:83:103

Output:

Run options: include {:locations=>{"./spec/models/company_spec.rb"=>[81, 82, 83, 103]}}

Solution 6 - Ruby

As of RSpec 2.4 (I guess) you can prepend an f or x to it, specify, describe and context:

fit 'run only this example' do ... end
xit 'do not run this example' do ... end

http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#fit-class_method http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#xit-class_method

Be sure to have config.filter_run focus: true and config.run_all_when_everything_filtered = true in your spec_helper.rb.

Solution 7 - Ruby

In newer versions of RSpec, it's even easier to configure support fit:

# spec_helper.rb

# PREFERRED
RSpec.configure do |c|
  c.filter_run_when_matching :focus
end

# DEPRECATED
RSpec.configure do |c|
  c.filter_run focus: true
  c.run_all_when_everything_filtered = true
end

See:

https://relishapp.com/rspec/rspec-core/docs/filtering/filter-run-when-matching

https://relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/run-all-when-everything-filtered

Solution 8 - Ruby

Also you can run specs which have focus: true by default

spec/spec_helper.rb

RSpec.configure do |c|
  c.filter_run focus: true
  c.run_all_when_everything_filtered = true
end

Then simply run

$ rspec

and only focused test will be run

then when you remove focus: true all tests well be run again

More information: https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/filtering/inclusion-filters

Solution 9 - Ruby

You can run as rspec spec/models/user_spec.rb -e "SomeContext won't run this".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan LongView Question on Stackoverflow
Solution 1 - RubyzeteticView Answer on Stackoverflow
Solution 2 - RubyJan MinárikView Answer on Stackoverflow
Solution 3 - RubyTom ChapinView Answer on Stackoverflow
Solution 4 - RubyAlex LangView Answer on Stackoverflow
Solution 5 - RubyJonathon BatsonView Answer on Stackoverflow
Solution 6 - RubyJoshua MuheimView Answer on Stackoverflow
Solution 7 - RubyjwfearnView Answer on Stackoverflow
Solution 8 - RubyitsnikolayView Answer on Stackoverflow
Solution 9 - RubyAvijit MajhiView Answer on Stackoverflow