setup and teardown for a ruby TestSuite
June 9, 2007
My Watir tests for a particular application are grouped into three subclasses of Test::Unit::TestCase. To run them all I have a top-level test suite that looks like this:
require 'test/unit' server.start require 'test/first_tester' require 'test/second_tester' require 'test/third_tester' server.stop
But this doesn’t work as intended, because the server.stop line at the end of the script is executed before the test suite is constructed and executed, which is obviously not what I want. The problem is in test/unit: the require causes this script to collect every test method in the ObjectSpace and then invoke the runner on the resulting suite.
What I would like is to have setup and teardown methods on TestSuite; but they aren’t there. I feel sure someone somewhere must have done this already (the closest I could find was this old post on ruby-talk), but I couldn’t find one quickly so I wrote my own:
require 'test/unit/testsuite'
require 'test/unit/ui/console/testrunner'
require 'test/first_tester'
require 'test/second_tester'
require 'test/third_tester'
class TS_MyTestSuite < Test::Unit::TestSuite
def self.suite
result = self.new(self.class.name)
result << FirstTester.suite
result << SecondTester.suite
result << ThirdTester.suite
return result
end
def setup
server.start
end
def teardown
server.stop
end
def run(*args)
setup
super
teardown
end
end
Test::Unit::UI::Console::TestRunner.run(TS_MyTestSuite)
Please let me know if this solution - or anything equivalent - is already published elsewhere, because I hate re-inventing wheels…







June 10, 2007 at 12:45 am
Try this:
at_exit {p ’server.stop’}
require ‘test/unit’
p ’server.start’
require ‘a_test’
I’ve used this several times. Because of the way that test/unit runs test cases (in an at_exit) and how at_exits get unrolled this works
-andy