如何从 RSpec 获取当前测试文件名? [英] How to get the current test filename from RSpec?
问题描述
我正在尝试加快大型 RSpec 项目的测试速度.除了使用 RSpec 的 --profile
选项之外,我还想打印出运行时间最长的测试文件 [1].
I'm trying to speed up a large RSpec project's tests. In addition to using RSpec's --profile
option I wanted to get the longest running test files [1] printed out.
在我的 spec_helper.rb
中,我将正在测试的类和总时间转储到一个文件中,但是因为我们有 spec/model
和 spec/request
目录我真的希望能够打印当前测试的 filename 而不仅仅是类名 (written_class
),以便用户可以消除 code>model/foo_spec.rb 和 request/foo_spec.rb
优化时.
In my spec_helper.rb
I dump the classes being tested and total time to a file, however as we have spec/model
and spec/request
directories I'd really like to be able to print the current test's filename and not just the class name (described_class
), so that the user can disambiguate between model/foo_spec.rb
and request/foo_spec.rb
when optimizing.
在 spec/spec_helper.rb
的 before
块中,如何获取当前测试文件的文件名?
In a before
block in the spec/spec_helper.rb
, how can I get the current test file's filename?
我的(经过大量修剪的)spec_helper
看起来像这样:
My (heavily trimmed) spec_helper
looks like this:
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 元数据中似乎不可用,但我希望更熟悉内部结构的人可以想出一种方法来公开它.谢谢,戴夫
This doesn't seem to be available in the curretn RSpec meta-data, but I'm hoping someone more familiar with the internals can think of a way to expose it. Thanks, Dave
[1] 通常,包含 100 个示例的文件比 --profile 中的单个示例产生的速度更快 - 当该大文件的 before :each
/before :所有
块都是有针对性的,显然即使是保存的毫秒数也会乘以文件中的测试数.除了 --profile
之外,使用这种技术对我帮助很大.
[1] Often a file with, say, 100 examples in it yields more speed up than a single example from --profile - when that large file's before :each
/ before :all
blocks are targetted, obviously even a ms saved is multiplied up by the number of tests in the file. Using this technique in addition to --profile
helped me a lot.
推荐答案
只要你只是用它来分析你的测试以确定哪些文件需要改进,你应该能够将它扔到你的 spec_helper.rb
文件(然后将其删除).我完全理解这在生产环境中并不漂亮/干净/优雅/可接受,我否认我曾经写过它:)
As long as you're just using this for profiling your tests to identify which files need to be improved, you should be able to toss this into your spec_helper.rb
file (and remove it afterwards). I fully understand that this is not pretty/clean/elegant/acceptible in production environments and I disavow that I ever wrote it :)
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
这篇关于如何从 RSpec 获取当前测试文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!