Thursday, March 02, 2006

Execute all Test::Unit tests in a directory

PickAxe gives an example of how to create test suites in Ruby. It's been awhile since I read it, but if I remember correctly it involves simply adding a require 'test_name' for each test you want to run in your suite. This does work, but it can become a maintenance pain.

When developing a Ruby application I generally put all my tests in a Test folder or in folders that are descendants of Test. When I want to execute all the tests, I simply need to require all files that are in or below Test.
require 'find'

Find.find(File.dirname(__FILE__)) do |path|
Find.prune if path =~ /#{File.expand_path('.')}\/#{__FILE__}$/
require path.reverse.chomp(File.expand_path('.').reverse).chomp('/').reverse.chomp('.rb') if !File.directory?(path)
end
That's it. All files below Test will be required and therefore executed.
Post a Comment