How do I run only specific tests in Rspec?

85,359

Solution 1

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

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

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

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

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]}}
Share:
85,359

Related videos on Youtube

Nathan Long
Author by

Nathan Long

I code mostly in Elixir, sometimes Ruby. More about me at nathanmlong.com and Stackoverflow Careers.

Updated on January 14, 2021

Comments

  • Nathan Long
    Nathan Long over 3 years

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

  • tir38
    tir38 about 11 years
    So you don't have to go searching, the direct link to zetetic's suggestion is here (for Rspec 2.12) relishapp.com/rspec/rspec-core/v/2-12/docs/command-line/…
  • jwg2s
    jwg2s over 10 years
    We added a spec to our suite to ensure code never gets merged with focus: true still in source control. gist.github.com/jwg2s/7361603
  • zetetic
    zetetic over 10 years
    @jwg2s I use a git hook to block commits with :focus, which also prevents undesirables like 'binding.pry, console.log`, etc. from creeping in to the codebase.
  • Otheus
    Otheus almost 8 years
    Are you involved with rspec's documentation efforts?
  • Otheus
    Otheus almost 8 years
    Is spec/spec_helper.rb always included ? Or only if no options are given? Why do test modules have require 'spec_helber', and doesn't having the above code eliminate the possibility of running a single test by specifying the file? I can't find any documentation on this.
  • zetetic
    zetetic almost 8 years
    @Otheus no, I'm just a fan :) I really like what they did on Relish, but SO just launched its own documentation feature, so we may see some competition.
  • Otheus
    Otheus almost 8 years
    Maybe you can point me in the way of documentation that actually describes usage and actual behavior of the rspec program :) Because the Relish doc does not.
  • Ali Ghanavatian
    Ali Ghanavatian over 7 years
    In rspec 3.5, it is config.filter_run_when_matching and it could work just by adding :focus to the example
  • Kris
    Kris over 6 years
    spec_helper.rb is always included if you have --require spec_helper in .rspec in the project root.
  • zach
    zach over 6 years
    If 'focus: true' is accidentally committed your CI will be passing despite not running most of the tests.
  • Igbanam
    Igbanam almost 6 years
    This is so gold! 🙇🏾‍♂️