2016-03-14 34 views
0

RSpec有没有一种方法可以显示每个测试持续时间,而不仅仅是整个套件持续时间?单一规格持续时间

现在我们有

7分31秒结束时(文件了4.71秒加载)

,但我想有一些像

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) 

回答

4

你可以使用rspec --profile N,这会向您显示前N个最慢的示例。

+0

是的,我可以,但是如果我在我的测试套件中有100个示例,它不会帮助我实现我想要的。 – fabersky

+0

@fabersky你可以把任何数字与--profile一起配置N个例子,查看 –

+0

好的。我低估了,因为在你的第一个答案中,你没有把“--profile” – fabersky

2

快速解决方案请参阅@ maximf的答案。对于另一种解决方案,你可以编写你自己的rspec格式化器,这会让你更好地控制你正在测量的内容。

例如,extnding的RSpec的基础文本格式:

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 

注意这个只登录过的例子次,但你可以添加一个example_failed没有做类似的,它也只能使用RSpec 3。这是基于我的工作我自己格式化:https://github.com/yule/dots-formatter

0

而不是做rspec --profile N每次我们运行规格(如@maximf说),我们可以把它添加到我们的RSpec的配置:

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