2014-02-10 146 views
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:functionalstest:integration,甚至是test:units TEST='test/unit/an_example_test.rb'。但是,如果我没有指定test:units的测试,则TESTOPTS似乎被忽略。

回答

0

在经典SO风格,我找到了答案清楚阐明对自己以后,所以这里是:

当没有TEST=/test/unit/blah_test.rb运行,test:units TESTOPTS=需要其内容之前--。因此,在整体解决方案很简单:

rake test:units TESTOPTS='-- --runner=profiling'

+0

这是为什么在这种情况下需要的是超越我,所以,如果有人可以帮我找到这个问题的答案,将不胜感激。 – RubberDucky