How to Run a Single Test/Spec File in Rspec

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

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

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 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 test in guard for rspec?

You have to argument your spec/spec_helper.rb file to accept the :focus => true statement.

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

Then you can use

it 'does something', :focus => true do
//your spec
end

or

describe "something", :focus => true do
before do
sign in
visit page
end

it { does something }
it { does something else }
end

If you are going to take that approach, you probably also want to ensure all specs are run if there is no :focus => true anywhere, using the documented approach:

RSpec.configure do |config|
config.run_all_when_everything_filtered = true
end

You can do a lot with filters; you might want to have a look at this page: https://relishapp.com/rspec/rspec-core/v/3-0/docs/filtering

Using Rspec in one file like test/unit

You just need to tell the RSpec::Core::Runner to run your spec.

Adding RSpec::Core::Runner.run([$__FILE__]) at the end of your fill should work.

Updated code:

require 'rspec'

describe "Anagrams" do
def anagrams(word, words)
words.select { |w| w.chars.sort == word.chars.sort }
end

it "should only match the anagrams" do
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) == ['aabb', 'bbaa']
end
end

RSpec::Core::Runner.run([$__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

Run each RSpec spec file individually

Assuming you are on MacOs/Linux, specs are in ./spec and suffixed _spec.rb:

find spec -name '*_spec.rb' -exec bundle exec rspec {} \;

or

find spec -name '*_spec.rb' | xargs -I {} bundle exec rspec {}


Related Topics



Leave a reply



Submit