fabdurso
fabdurso

Reputation: 2444

Single spec duration

Is there a way in RSpec to show every single test duration and not just the total suite duration?

Now we have

Finished in 7 minutes 31 seconds (files took 4.71 seconds to load)

but I'd like to have something like

User accesses home and
 he can sign up (finished in 1.30 seconds)
 he can visit profile (finished in 3 seconds)
 .
 .
 .
Finished in 7 minutes 31 seconds (files took 4.71 seconds to load)

Upvotes: 1

Views: 72

Answers (3)

fabdurso
fabdurso

Reputation: 2444

Instead of doing rspec --profile Neverytime we run specs (as @maximf said), we can add it to our RSpec configuration:

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

Upvotes: 0

Yule
Yule

Reputation: 9764

For a quick solution see @maximf's answer. For an alternative solution, you could write your own rspec formatter, which would give you greater control over what you are measuring.

For example, extnding rspec's base text formatter:

RSpec::Support.require_rpec_core "formatters/base_text_formatter"
module RSpec::Core::Formatters
  class TimeFormatter < BaseTextFormatter
    Formatters.register self, :example_started, :example_passed

    attr_accessor :example_start, :longest_example, :longest_time

    def initialize(output)
      @longest_time = 0
      super(output)
    end

    def example_started(example)
       @example_start = Time.now
       super(example)
    end

    def example_passed(example)
       time_taken = Time.now - @example_start
       output.puts "Finished #{example.example.full_description} and took @{Helpers.format_duration(time_taken)}"
       if @time_taken > @longest_time
         @longest_example = example
         @longest_time = time_taken
       end
       super(example)
     end

     def dump_summary(summary) 
       super(summary)
       output.puts 
       output.puts "The longest example was #{@longest_example.example.full_Description} at #{Helpers.format_duration(@longest_time)}"
     end
   end
end

Note that this will only log times on passed examples, but you could add an example_failed failed to do similar, it also only works with RSpec 3. This is based on my work on my own formatter: https://github.com/yule/dots-formatter

Upvotes: 2

Max Filippov
Max Filippov

Reputation: 2082

You can use rspec --profile N, which would show you the top N slowest examples.

Upvotes: 5

Related Questions