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 look for this the file at:

    /usr/lib/ruby/1.8/test/unit/testcase.rb    OR

    /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
 test_time = ENV["TEST_TIME"]     # added
 start_time = Time.now if test_time   # added
	begin	#  ...  stuff skipped ...
 	end	
        result.add_run	if test_time	 # added
           elapse = Time.now - start_time # added 
	   puts " #{self.class.to_s}.#{@methodname} : #{elapse}" if elapse > test_time.to_f  # added
	end 	 # added
        yield(FINISHED, name)
	end

-- Mark Windholtz

Read more at our BLOG.