快速解决方案请参阅@ 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
是的,我可以,但是如果我在我的测试套件中有100个示例,它不会帮助我实现我想要的。 – fabersky
@fabersky你可以把任何数字与--profile一起配置N个例子,查看 –
好的。我低估了,因为在你的第一个答案中,你没有把“--profile” – fabersky