How do I run only specific tests in Rspec?
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.
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
You can tag examples with :focus
hash attribute. For example,
# spec/foo_spec.rb
RSpec.describe Foo do
it 'is never executed' do
raise "never reached"
end
it 'runs this spec', focus: true do
expect(1).to eq(1)
end
end
rspec --tag focus spec/foo_spec.rb
More info on GitHub. (anyone with a better link, please advise)
(update)
RSpec is now superbly documented on relishapp.com. 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.