34

How can I find a list of the slowest rspec tests? I want to refactor those to run faster. I tried looking for gems but can't find any. I was thinking of putting something in

Rspec.before(:each)

and

Rspec.after(:each)

blocks to generate this list. But I don't know how to access the name of the spec.

Ibrahim Muhammad
  • 2,808
  • 4
  • 29
  • 39

2 Answers2

74

That's actually built right in to the rspec command. Just use the -p or --profile flag while you're running all your rspec tests.

MrDanA
  • 11,489
  • 2
  • 36
  • 47
15

As MrDan says, the --profile flag. You can also make a .rspec file in the root of your project, so that you get the timing information all the time, like this:

$ cat .rspec 
--colour
--profile
--format nested
--backtrace

You can also speed up all your tests generally by turning off garbage collection, like this:

# turn off garbage collection when running tests, place this in spec_helper.rb
RSpec.configure do |config|
  config.before(:all) do
     DeferredGarbageCollection.start
  end

  config.after(:all) do
     DeferredGarbageCollection.reconsider
  end
end
Rob
  • 4,404
  • 2
  • 32
  • 33