【发布时间】:2012-03-27 13:57:14
【问题描述】:
我正在尝试加快大型 RSpec 项目的测试。除了使用 RSpec 的 --profile 选项之外,我还想打印出运行时间最长的测试文件 [1]。
在我的spec_helper.rb 中,我将正在测试的类和总时间转储到一个文件中,但是由于我们有spec/model 和spec/request 目录,我真的希望能够打印当前测试的文件名 而不仅仅是类名(described_class),以便用户在优化时可以消除model/foo_spec.rb 和request/foo_spec.rb 之间的歧义。
在spec/spec_helper.rb 的before 块中,如何获取当前测试文件的文件名?
我的(严重修剪)spec_helper 看起来像这样:
config.before :all do
@start_time = Time.now
end
config.after :all do |test|
timings.push({ :name => test.described_class,
:file => 'I do not know how to get this',
:duration_in_seconds => (Time.now - @start_time) })
end
config.after :suite do
timing_logfile_name = 'log/rspec_file_times.log'
timing_logfile = "#{File.dirname(__FILE__)}/../#{timing_logfile_name}"
file = File.open(timing_logfile, 'w')
timings.sort_by{ |timing| timing[:duration_in_seconds].to_f }.reverse!.each do |timing|
file.write( sprintf("%-25.25s % 9.3f seconds\n",
timing[:name], timing[:duration_in_seconds]) )
end
file.close
tell_if_verbose("Overall test times are logged in '#{timing_logfile_name}'")
end
这似乎在当前的 RSpec 元数据中不可用,但我希望更熟悉内部结构的人能想出一种方法来公开它。谢谢, 戴夫
[1] 通常,一个包含 100 个示例的文件比来自 --profile 的单个示例产生更快的速度 - 当以该大文件的 before :each / before :all 块为目标时,显然甚至节省了一毫秒乘以文件中的测试数。除了--profile 之外,使用这种技术对我有很大帮助。
【问题讨论】:
标签: ruby-on-rails ruby rspec rspec2