I think there's a way to run only tests with a given label. Anybody know?
10 Answers
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.

- 18,137
- 13
- 50
- 91

- 47,184
- 10
- 111
- 119
-
So you don't have to go searching, the direct link to zetetic's suggestion is here (for Rspec 2.12) https://www.relishapp.com/rspec/rspec-core/v/2-12/docs/command-line/tag-option – tir38 Apr 10 '13 at 19:31
-
We added a spec to our suite to ensure code never gets merged with focus: true still in source control. https://gist.github.com/jwg2s/7361603 – jwg2s Nov 07 '13 at 20:44
-
@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. – zetetic Nov 07 '13 at 21:35
-
Are you involved with rspec's documentation efforts? – Otheus Jul 20 '16 at 17:55
-
1@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. – zetetic Jul 22 '16 at 05:55
-
1Maybe 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. – Otheus Jul 22 '16 at 15:22
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.

- 742
- 1
- 10
- 18

- 3,227
- 1
- 17
- 23
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

- 29,210
- 11
- 96
- 131

- 3,276
- 1
- 29
- 18
-
2In rspec 3.5, it is `config.filter_run_when_matching` and it could work just by adding `:focus` to the example – Ali Ghanavatian Sep 10 '16 at 07:24
-
5If 'focus: true' is accidentally committed your CI will be passing despite not running most of the tests. – zach Oct 19 '17 at 05:36
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)

- 1,298
- 11
- 14
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]}}

- 9,493
- 8
- 43
- 66

- 1,191
- 11
- 13
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
.

- 12,617
- 9
- 76
- 152
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

- 28,781
- 28
- 95
- 122
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

- 17,415
- 4
- 65
- 64
-
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. – Otheus Jul 20 '16 at 17:57
-
1`spec_helper.rb` is always included if you have `--require spec_helper` in `.rspec` in the project root. – Kris Aug 04 '17 at 11:52
You can run as rspec spec/models/user_spec.rb -e "SomeContext won't run this"
.

- 8,296
- 3
- 31
- 46

- 508
- 9
- 15
You can simply run by any metadata using filter_run_including
and filter_run_excluding
. It allows more flexibility
For example below line will allow running only Rails system tests
config.filter_run_including type: :system
And this line will allow running everything except Rails system tests
config.filter_run_excluding type: :system

- 563
- 5
- 13