0
我最近决定为我们的Rails 3.0应用的测试套件编写一个简单的测试运行时分析器。这是一个非常简单(读:哈克)脚本,每次测试的时间增加了一个全球性的,然后将结果在运行结束时输出:覆盖rake测试:单元跑步者
require 'test/unit/ui/console/testrunner'
module ProfilingHelper
def self.included mod
$test_times ||= []
mod.class_eval do
setup :setup_profiling
def setup_profiling
@test_start_time = Time.now
end
teardown :teardown_profiling
def teardown_profiling
@test_took_time = Time.now - @test_start_time
$test_times << [name, @test_took_time]
end
end
end
end
class ProfilingRunner < Test::Unit::UI::Console::TestRunner
def finished(elapsed_time)
super
tests = $test_times.sort{|x,y| y[1] <=> x[1]}.first(100)
output("Top 100 slowest tests:")
tests.each do |t|
output("#{t[1].round(2)}s: \t #{t[0]}")
end
end
end
Test::Unit::AutoRunner::RUNNERS[:profiling] = proc do |r|
ProfilingRunner
end
这让我跑套房像这样rake test:xxx TESTOPTS="--runner=profiling"
并获得附加到默认跑步者输出结尾的前100名测试列表。它适用于test:functionals
和test:integration
,甚至是test:units TEST='test/unit/an_example_test.rb'
。但是,如果我没有指定test:units
的测试,则TESTOPTS似乎被忽略。
这是为什么在这种情况下需要的是超越我,所以,如果有人可以帮我找到这个问题的答案,将不胜感激。 – RubberDucky