Friday, January 06, 2006

Creating Test Suites In Ruby

The following is one way to create a suite of tests automatically in Ruby. There are probably better ways, but this works for me.


require 'test/unit'
require 'test/unit/testsuite'
require 'test/unit/ui/gtk2/testrunner'
require 'test/unit/ui/console/testrunner'

# The initial definition of AllTests is here so
# that we can refer to it in our redefinition of
# Class#inherited

class AllTests
TESTS = Array.new
end

class Class
def inherited(arg)
if arg < Test::Unit::TestCase
AllTests::TESTS << arg
end
end
end

Dir.glob('*test.rb').each {|f|
require f
}

class AllTests

def self.suite
suite = Test::Unit::TestSuite.new('All Tests')
TESTS.each {|t| suite << t.suite}
suite
end

end

if __FILE__ == $0
Test::Unit::UI::GTK2::TestRunner.run(AllTests)
else
Test::Unit::UI::Console::TestRunner.run(AllTests)
end

0 Comments:

Post a Comment

<< Home