Dav*_*edy 4 ruby rspec ruby-on-rails rspec2
我正在努力加快一个大的RSpec项目的测试.除了使用RSpec --profile选项之外,我还希望打印出运行时间最长的测试文件[1].
在我的spec_helper.rb转储被测试的类和文件的总时间,但是因为我们spec/model和spec/request目录我真的希望能够打印当前测试的文件名而不仅仅是类名(described_class),以便用户可以消除歧义在优化之间model/foo_spec.rb和之间request/foo_spec.rb.
在一个before块中spec/spec_helper.rb,如何获取当前测试文件的文件名?
我(严重修剪)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
Run Code Online (Sandbox Code Playgroud)
这在当前的RSpec元数据中似乎不可用,但我希望有更熟悉内部的人可以想出一种揭露它的方法.谢谢,戴夫
[1]通常一个带有100个例子的文件比--profile中的单个例子产生更快的速度 - 当大文件的before :each/ before :all块被定位时,显然即使节省的ms也乘以测试的数量.文件.使用这种技术除了--profile帮助了我很多.
pla*_*mbo 10
只要您只是使用它来分析测试以确定哪些文件需要改进,您应该能够将其丢入您的spec_helper.rb文件中(之后将其删除).我完全明白,这在生产环境中并不漂亮/干净/优雅/可以接受,我否认我写过它:)
config.before(:each) do |example|
path = example.metadata[:example_group][:file_path]
curr_path = config.instance_variable_get(:@curr_file_path)
if (curr_path.nil? || path != curr_path)
config.instance_variable_set(:@curr_file_path, path)
puts path
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4056 次 |
| 最近记录: |