Run Rspec Tasks in a Specific Order

Run RSpec tasks in a specific order

Check this http://blog.davidchelimsky.net/2012/01/04/rspec-28-is-released/

–order rand We added an --order option with two supported values: rand
and default.

rspec --order random (or rand) tells RSpec to run the groups in a
random order, and then run the examples within each group in random
order. We implemented it this way (rather than complete randomization
of every example) because we don’t want to re-run expensive
before(:all) hooks. A fair tradeoff, as the resulting randomization is
just as effective at exposing order-dependency bugs.

When you use --order random, RSpec prints out the random number it
used to seed the randomizer. When you think you’ve found an
order-dependency bug, you can pass the seed along and the order will
remain consistent:

--order rand:3455
--order default tells RSpec to load groups and examples as they are declared in each file

Basically you should order your tests in a spec file an then execute it with --order default option.
.

How to execute all contexts in the defined order in RSPEC

rspec will execute tests within a spec file in the defined order if you call it as follows:

rspec --order defined

You can also tag the example group (the one you specified with describe) with the meta-data order: :defined to do the same thing.

However, this only works for the test within the file. rspec will execute the files in alphabetical order. This is a current restriction in the program, based on this issue report where the author states: "RSpec does not execute tests in alphabetical order. It loads spec files in alphabetical order and then runs tests in defined order or random order depending on your configuration."

You execute each file in a single rspec command with a little bash magic, but it may or may not be worth the effort:

echo "file4 file3 file1 file2" |
xargs -n1 -t bundle exec rspec --format doc --order defined

Running Rake tasks in Rspec Tests

You can invoke Rake tasks as following:

require 'rake'
Rake::Task[name].invoke

In this case this would result in the following code:

require 'rake'
Rake::Task['db:test:purge'].invoke

Running specific specs last

The only options for ordering examples within an rspec file are "default" (i.e. in the order declared) or "random" (with an option to specify a "seed" value), as described in Run RSpec tasks in a specific order and its references. However, when given a set of spec files to execute, RSpec executes those specs in the order specified. So, if you're willing to put your "last" spec in a separate file, you can always just specify it as the last spec/file to be executed.

Run some RSpec files in default order

I should've just read the specs, and not the code from the merged feature in the rspec core. It's pretty clear given this spec.

Given a custom_ordered_spec.rb:

    RSpec.configure do |config|
config.order_groups_and_examples do |list|
list.sort_by { |item| item.description }
end
end

describe 'group B' do
specify('group B example D') {}
specify('group B example B') {}
specify('group B example A') {}
specify('group B example C') {}
end

describe 'group A' do
specify('group A example 1') {}
end

Both groups and examples within groups will be ordered by description. Furthermore, it looks like you can call config.order_groups and config.order_examples.

Here's my blog post about it with a somewhat more detailed description.

How to obtain the order in which tests are executed when running RSpec with --order random?

Run rspec with a verbose format:

rspec --order random --format documentation

It will print all of the example descriptions as well as the seed.

There are html and json output formats as well, which also include the test names and so will tell you the order.

If you prefer brevity, -f d is the same as --format documentation and so on.

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