How to Run Only Specific Tests in Rspec

How do I run only specific tests in Rspec?

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.

How to run a single RSpec test?

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

RSpec.configure do |config|
config.filter_run_when_matching :focus
end

And then add a focus tag to the it, context or describe to run only that block:

it 'runs a test', :focus do
...test code
end

RSpec documentation:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

How do you run a single test/spec file in RSpec?

Or you can skip rake and use the 'rspec' command:

bundle exec rspec path/to/spec/file.rb

In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.

If you're using an older version of rspec it is:

bundle exec spec path/to/spec/file.rb

Rails Rspec - Run only single test

Try this to run a single test:

rspec ./spec/parent/child/spec.rb:line_number_of_your_test

line_number_of_your_test can be any number spanning the test if your it block is a multi-line block.

Update: Removed 'rand:seed' part.

How do I run only specific Rspec tests in rails?

If you want to run all your specs in your spec folder

rspec spec

If you want to run all the specs in your user_spec.rb

rspec spec/models/user_spec.rb

If you want to run one spec from user_spec.rb on line 29

rspec spec/models/user_spec.rb -l 29

If you want to run all specs tagged with mytag

rspec spec --tag mytag

it "is not valid without a name", :mytag do
user = Factory.build(:User, :name => nil)
user.should_not be_valid
end

If your using bundler you will need to use

bundle exec rspec

How to run multiple specific RSpec tests?

You can use the following syntax to run your specs for multiple files:

rspec path/to/first/file path/to/second/file

Can rspec be configured to only run tests what have been modified within a single spec file?

I think you are looking for guard.

Checkout nicely written article https://collectiveidea.com/blog/archives/2017/02/09/guard-is-your-friend

Rspec run all tests except a specific folder

Use an --exclude-pattern, they're quite convenient:

https://www.relishapp.com/rspec/rspec-core/v/3-3/docs/configuration/exclude-pattern

One of the nice things about them:

The --exclude-pattern flag accepts shell style glob unions

So you could do something like rspec --exclude-pattern "spec/{foldername1,foldername2}/**/*_spec.rb"



Related Topics



Leave a reply



Submit