Test::Unit timer

Test::Unit command line runner shows progress by outputing a dot '.' for every test thats completed sucessfully.

I wanted to know which in that line of dots was going slow.

My solution was to hack into Test::Unit and add a time limit argument. If the test takes longer than the time limit it prints the name of the test and the elapse time.

To get Test::Unit to report tests taking excessive time, define the environment variable TEST_TIME to whatever time you want.

  • export TEST_TIME=1.0; rake

This will report every test that takes longer than 1.0 seconds to run.

  • rake TEST_TIME=0.0

This will report times for all tests

Here's the code


  • on OS/X the file I need is at: /opt/local/lib/ruby/1.8/test/unit/testcase.rb

  • add the commented lines to #run

  • Heres the run method with the middle chopped out 


def run(result)	
  yield(STARTED, name)	
  @_result = result	
  testtime = ENV["TESTTIME"]        # added	
  starttime = Time.now if testtime   # added	
  begin	
  #  ...  stuff skipped ... 	
  end	
  result.add_run	
  if test_time	
    elapse = Time.now - start_time # added 	
   puts " #{self.class.tos}.#{@methodname} : #{elapse}" if elapse > testtime.tof  # added	
  end 	
  yield(FINISHED, name)	
end

Created at: Wed Jul 19 11:01:00 UTC 2006 Updated at: Wed Feb 20 21:32:51 UTC 2008