Ordering - Fast Specs First

431 views
Skip to first unread message

AndyL

unread,
Feb 26, 2015, 10:48:52 AM2/26/15
to rs...@googlegroups.com
My unit/models specs run in a couple seconds, but my integration/feature specs take minutes.

I'd like to always run the fast specs before the slow specs.

I try some test cases:

    rspec models/spec1_spec.rb features/spec2_spec.rb
    rspec features/spec2_spec.rb models/spec1_spec.rb

and discover that the features always run before the models. 

It seems that rspec sorts the specs by path name before running.

Is there any way to force Rspec to run my model specs before the feature specs?

Myron Marston

unread,
Feb 26, 2015, 4:08:59 PM2/26/15
to rs...@googlegroups.com
The simplest way is to just do:

`rspec models && rspec features` to run one followed by the other.  If you want to boot RSpec only once, you can use RSpec's ordering API to order your specs arbitrarily:


RSpec.configure do |config|
  config.register_ordering(:global) do |list|
    # put logic in here to order model specs before feature specs
  end
end

HTH,
Myron

 

AndyL

unread,
Feb 27, 2015, 2:49:26 PM2/27/15
to rs...@googlegroups.com
Thanks for your reply Myron -

I'd like to boot rspec just once.  I tried using the global ordering technique, but it looks to me like it is called once per spec file, populating the 'list' argument with the list of example groups in that file.

I'm interested in reordering the list of files to be run, not the example groups within a file.  Is there a way to reorder files?

Myron Marston

unread,
Feb 27, 2015, 10:53:48 PM2/27/15
to rs...@googlegroups.com
On Friday, February 27, 2015 at 11:49:26 AM UTC-8, AndyL wrote:
Thanks for your reply Myron -

I'd like to boot rspec just once.  I tried using the global ordering technique, but it looks to me like it is called once per spec file, populating the 'list' argument with the list of example groups in that file.

I'm interested in reordering the list of files to be run, not the example groups within a file.  Is there a way to reorder files?

It's called one time per level of nesting -- one time with all top level groups, and then one time per group (at any level) with its nested examples and groups.  It doesn't split things by file at all, unless you organize things so that you have one example group per file.

And to clarify a bit: to RSpec, files only have meaning in that they are what it loads.  Once it's loaded the spec files it deals only in the in-memory example groups and examples, and reording files isn't even a concept that would make sense.

Myron

AndyL

unread,
Feb 28, 2015, 1:25:39 AM2/28/15
to rs...@googlegroups.com
Thanks for explaining - enormously helpful.  Here's my solution:

RSpec.configure do |config|
config.register_ordering(:global) do |items|
items.sort_by do |group|
case group.metadata[:type]
when :feature then 50
when :request then 40
when :model then 10
else 20
end
end
end
end

Myron Marston

unread,
Feb 28, 2015, 2:21:55 AM2/28/15
to rs...@googlegroups.com
On Friday, February 27, 2015 at 10:25:39 PM UTC-8, AndyL wrote:
Thanks for explaining - enormously helpful.  Here's my solution:

RSpec.configure do |config|
config.register_ordering(:global) do |items|
items.sort_by do |group|
case group.metadata[:type]
when :feature then 50
when :request then 40
when :model then 10
else 20
end
end
end
end

Looks like a real simple solution!

Chris Irish

unread,
Feb 28, 2015, 2:48:01 PM2/28/15
to rs...@googlegroups.com
You could tag your slow specs

scenario 'some test', slow: true do 
  # stuff
end

then in your .rspec file add

--tag ~slow

which says don't run slow tagged specs by default.  then when you do want to run the slow ones just do

rspec /spec --tag slow

AndyL

unread,
Feb 28, 2015, 3:44:27 PM2/28/15
to rs...@googlegroups.com
Yes the modification was simple!  Respect to the rspec designers.  I've been using this for a half-day now & find it very helpful.  Thanks again for the tips.

AndyL

unread,
Feb 28, 2015, 3:49:04 PM2/28/15
to rs...@googlegroups.com
Yeah - I've been using tags heavily for a long time - they are awesome.  This is the first time I've tried to use tags to change the default execution order & was happy to find how easy to customize.
Reply all
Reply to author
Forward
0 new messages