I have some slow RSpec tests that I've tagged with :slow. I've set RSpec up to skip them default by adding the line config.filter_run_excluding slow: true to my RSpec configuration. And I can run the slow tests by running rspec --tag slow.
But how can I run all the tests using one command, including slow and non-slow tests? I can't figure it out from the docs.
You can find a similar question here : Command line to run all examples in RSpec, including ones that are filtered out?
In few words, this feature doesn't exist on rspec but you can use env variable :
RSpec.configure do |c|
c.filter_run_excluding slow: true unless ENV['ALL']
end
Call ALL=1 rspec will run all the specs including the slow tag.
Related
I'm working with Rails 5 and rspec (gem version 4). I was wondering if RSpec can be configured to only run tests that have been modified within a single file when only running that file, i.e.
bundle exec rspec spec/my_spec.rb
. If my file is like this
RSpec.describe MyClass do
context "context 1" do
it "tests condition 1" do
end
it "tests condition 2" do
end
...
end
context "context 2" do
...
end
...
end
and I only update tests in "context 1," is it possible to test the single file and have only modified tests from within that file run? With respect to this answer -- Can I get RSpec to only run changed specs?, it appears that only relates to actual files that have changed when running the complete suite of rspec tests.
I think you are looking for guard.
Checkout nicely written article https://collectiveidea.com/blog/archives/2017/02/09/guard-is-your-friend
There is a Ruby Gem called retest designed to do exactly that. Just run retest, and it will watch for changes in the code or the specs themselves, and rerun just the respective spec file.
We have a set of controller specs to ensure that our api is working as anticipated.
I'd also like to add a performance test that is not run by default but could be run (perhaps even as simply as via the line number like rspec spec/controllers/item_controller.rb:50. Some of these could be a couple of seconds so we don't want to be running every time (4 seconds x 30 actions adds up).
Is there a way to exclude a spec or describe block by default but have it be run via line number?
Add a tag to each of your slow tests to identify them, e.g.,
it 'should do something', slow: true do
#does something
end
Then simply run rspec with the exclusive tag option:
rspec --tag ~slow
This will run all tests that don't have the slow tag associated with them. To run the slow tests in conjunction with your other tests simply invoke without the tag option.
It might be preferable to have this as the default behaviour, in which case modify the .rspec configuration file and add the following to it:
--tag ~slow
Now by default when you simply run rspec all tests will run except the ones tagged as being slow. To run the slow tests explicitly set the flag:
rspec --tag slow
Though this will exclude all tests that aren't tagged as being slow.
When testing lib code there is rarely any need to require spec_helper and load all of rails. This is why I have been removing require "spec_helper" in favour of require_relative "../../lib/my_lib.rb".
These tests pass when called directly (rspec spec/lib/my_lib.rb) and are blazingly fast. Winner.
My issue comes when I try and run these tests as a group.
When I call rspec spec/lib it runs any lib specs that have a require "spec_helper" line but not any tests that don't.
I have played with spec_helper.rb to load in these tests, and that kind of works only it means that when I run rspec spec/models/blah.rb it will also run these lib tests, which obviously isn't what I want.
Is there a different way I should be calling my tests? Or is there a way I can get them added to the test run?
Notes
My spec_helper is configured to run the tests in a random order, I wonder if this has anything to do with it?
You should add _spec to your files:
spec/lib/my_lib_spec.rb
Rspec rake task will look only for files which end in _spec.
I've got an interesting problem that's causing myself and my team a lot of headaches when it comes to running our spec suite.
Our spec suite is broken up into the following sub-folders, and next to them is their total execution time to completion:
rspec spec/acceptance 311.67s
rspec spec/controllers 18.97s
rspec spec/decorators 4.39s
rspec spec/helpers 9.45s
rspec spec/lib 16.88s
rspec spec/mailers 5.27s
rspec spec/models 121.05s
rspec spec/presenters 0.03s
rspec spec/workers 19.3s
Total run time: 8m 27s
Which certainly could be improved, but all in all is pretty managable.
However, if, I run rspec spec and run the entire suite at once, the total time to complete is 27m 11s!
So, obviously something we are doing is dramatically affecting the performance of the entire suite when run at once. I'm hoping that I can get some pointers as to where I can begin to try to troubleshoot this problem.
If it helps, I've posted my spec_helper.rb file here
Thanks in advance,
Joe
At a guess, I'd say your integration specs are setting DatabaseCleaner to :truncation and it's not getting switched back to :transaction for the other specs. I have a sample spec_helper that navigates the situation here. There are also a couple methods provided that help you dig in and figure out which strategy is being used at any given time. Here it is for your convenience:
def cleaner_strategy
active_record_cleaner.instance_variable_get(:#strategy).class
end
def active_record_cleaner
DatabaseCleaner.instance_variable_get(:#cleaners)[[:active_record, {}]]
end
Side note: we use a filter for our integration specs since they are so slow. We disable them by default in our local env, and then let CI run them.
config.filter_run_excluding :slow unless ENV['SLOW']
Then you can run them individually with:
$ SLOW=true rspec spec/features/some_awesome_feature_spec.rb
What does profiling tell you?
Run rspec spec --profile or add --profile to your ~/.rspec file.
On completion of your tests, it will report the 10 slowest tests.
I've recently migrated from TextMate to RubyMine and have really liked it so far. One thing I noticed was that RubyMine doesn't have a Steak plugin like TextMate does, which allows the ability to run a single scenario.
Is there any way to do the same in RubyMine or am I stuck running the full file of acceptance tests everytime I want to run a single Steak test?
Steak is an extension for rspec. To run single tests in rspec you can specify a --tag
describe 'run this test', :focus => true do
# this one will run
end
describe 'but not this one' do
# wont get run
end
$ rspec --tag focus my_spec_file.rb
(Note: I've never used rubymine)