Show runtime for each rspec example
Asked Answered
S

2

37

currently I'm running more than 1k examples and it's taking a long time to complete (more than 20 minutes!!!).

I'd like to identify which examples are the ones taking more time to complete, is there any way to run rspec and return the time each example takes to complete(individually)? I'm using rspec 1.3.0 and rspec-rails 1.2.3

Shorn answered 31/1, 2011 at 21:46 Comment(1)
You may want to look at this: 37signals.com/svn/posts/2742-the-road-to-faster-testsAstro
G
58

You can use profiling to list your 10 slowest examples:

spec -p spec/*/*_spec.rb --colour --format profile

If you run this on a specific test suite you can get the 10 slowest examples from a smaller subset of examples:

spec -p spec/models/user_spec.rb --format profile

Newer Versions of Rspec

For newer versions of rspec you should use the --profile option:

rspec spec/ --profile         | (shows the 10 slowest examples)
rspec spec/ --profile 2       | (shows the 2  slowest examples)
Guerrilla answered 1/2, 2011 at 3:37 Comment(4)
Thanks! I think it should be easy to add support for the N slowest examples!Shorn
The "profile" format is not present in the version of Rspec I'm using, 2.11.0Mclaren
I think in the newer versions of RSpec you only need to use the --profile option.Guerrilla
you can have those params globally in ~/.rspec - a '--param stuff' per lineTimepiece
O
0

Add this line to the rails_helper.rb file

RSpec.configure do |config|
  config.profile_examples = true
end

now running rspec gives some output like this:

(times of each test ^^, then)
Finished in 2 minutes 25.1 seconds (files took 1.22 seconds to load)
71 examples, 0 failures, 15 pending
Octastyle answered 15/3 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.