Hoopla!

now with extra whiz-bang!

Hoopla!

Time your Rails tests

October 13, 2007 · 3 comments

As a project gets bigger the tests take longer. There’s no getting around that. So unless you want to abandon testing entirely it might help to see who’s eating all those cpu cycles.

in test/test_helper.rb:

class Test::Unit::TestCase

  def run_with_timing(*args, &block)
    @timer = Time.now
    run_without_timing(*args, &block)
    puts "#{Time.now - @timer} - #{self}" 
  end
  alias_method_chain :run, :timing if ENV['TIMER']

end
And run it like so:

$ rake TIMER=true
# or, to see the 10 slowest tests:
$ rake test:units TIMER=true 2>/dev/null | grep " - " | sort -r | head -n 10
And if you really want to air the dirty laundry:

$ grep -R 'def test_truth' test/ | grep -v .svn

Tags:··

3 responses so far ↓

  • 1 Ted // Oct 13, 2007 at 03:27 PM

    Note that your top ten command line example is just the slowest unit tests - I should know since I wrote it ;-P You’ll be hearing from my lawyers ;) Ha ha!

  • 2 topfunky // Oct 16, 2007 at 12:01 PM

    I published a gem that does something similar. It’s called test_timer:

    http://rubyforge.org/frs/shownotes.php?group_id=1513&release_id=7478

  • 3 Jack Danger // Oct 16, 2007 at 02:06 PM

    topfunky: I tried the version of that code that you had on your blog and for some reason it didn’t work or play well with mine. The gem works great though – thanks for pointing it out!

Leave a Comment