How to Define order of testcase files execution in Rspec? - ruby-on-rails

Is there a way in Rspec by which I can specify the order of exection by giving file input?
I have a .txt file which contain list of files in ordered manner. I want Rspec to execute testing of files in order specified in .txt file.
I am using CAPYBARA with Rspec (USING_CAPYBARA=true rspec -r turnip/rspec -r turnip/capybara spec/features/../../.)
EDIT:
Whenever the features fails on my CI tool, I get the list of file names in sequence which was executed by my CI tool. When I simply run my failing test it works fine, but to debug such issue I need to run the test files in same sequence as the CI tool. So I think if Rspec doesn't provide a way to rerun the specs files in specific sequence I guess I need to write some script for it then

RSpec tells you which seed it used to randomize the order. Look for something like:
Randomized with seed 47311.
You can use that number to re-run specs in the same order:
$ spec spec --seed 47311 # or --order rand:47311
From the documentation:
Use the --order option to tell RSpec how to order the files, groups, and
examples. The available ordering schemes are defined and rand.

Related

In Rails' rspec, how do I write/edit my test file so that examples in a specific context are run in a set order?

I’m using Rails 4.2 with RSpec 3.4.0. If I want to run all examples in the order in which the appear in a file, I can run
bundle exec rspec --order defined spec/models/my_model_spec.rb
But what if I only want to run the examples in a specific describe block in a specific order? It there any way I can do that by adding some attributes or making annotations in the file itself (as opposed to having to add them in on the command line)?
If you just want to run the specs within a single describe or context, just add the line number of the describe or context statement to the end of the spec file name.
bundle exec rspec --order defined scec/models/my_model_spec.rb:123
You can also run a specific example group by name using the --example option.
bundle exec rspec --example "context name" spec/models/my_model_spec.rb
Though, that second choice will use a regex to find the string. So, it will find all instances of that string in any describe, context, or it statement.

How to start all files of unit tests from one file using Rspec?

I have written unit testing of models of a Ruby on Rails app. I have tested all the spec files individually. Now I want to start all files from one file. Kindly guide me.
You can simply test all models files of specs by following way...
rspec --pattern spec/models/*_spec.rb --color

Run single system test

To run a single test in Rails, we normally do:
rails test TEST=test/system/invitation_test.rb
But that doesn't work with system tests. Neither do this work:
rails test:system TEST=test/system/invitation_test.rb
With both those commandos above, all system tests (files) are run.
So my question is, how can I run a single system test?
As a side note, to run (all) system tests in Rails, you need to append :system to test.
rails test:system
While rails test doesn't seem to work if you want to run your system tests (you need to append test with :system), if you only want to run a single test it does seem to work:
rails test test/system/my_little_test.rb

not run a spec by default unless flag is set

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.

rspec2 in rails 4.0.5 app skipping specs in one spec file

I wrote some new specs in a file called, spec/requests/schedule_revise_button_next_day.rb, and was able to test the specs (red to green) so long as I ran that file alone with:
$ rspec spec/requests/schedule_revise_button_next_day.rb
When I ran all of my specs, however, with $ rspec spec/ , that file would get skipped. The specs wouldn't be run. To test running the file with other spec files, I tried renaming the file with different prefixes like a_schedule_revise_button_next_day.rb and then running commands like:
$ rspec spec/requests/a*
That file would still get skipped though the other a* files would run.
I also isolated the problem to the file name since I was able to get the specs to run when I cut and pasted them into other files. I also was able to replicate the problem on two separate computers with different hardware and OS (OSX and Ubuntu.)
Only when I changed the file name to schedule_revise_spec.rb would it not be skipped when I ran it with all of my other specs. This situation makes me nervous in that I almost didn't realize that these specs were not being run. Anyone know what the problem is with my original file name? I'd be very grateful for any thoughts anyone has.
Best,
Ben
When given a directory or file pattern on the command line, RSpec looks for files ending in _spec. See How can I get Rspec to run all tests nested under a folder? for more discussion.

Resources