This post originated from an RSS feed registered with Ruby Buzz
by Jamis Buck.
Original Post: TestUnit Inheritance Hierarchies
Feed Title: the buckblogs here
Feed URL: http://weblog.jamisbuck.org/blog.cgi/programming/index.rss
Feed Description: Jamis Buck's corner of the blogging universe. Mostly about ruby, but includes ramblings on a variety of topics.
One of the current design features of Ruby’s TestUnit class is that every subclass of TestUnit must have at least one test method implemented. If that isn’t the case, you get a runtime error.
Sometimes, however, you’d like to use inheritance to set up a hierarchy of test “helpers”. For instance, you’d like to have some scaffolding to aid in integration testing, and you just want to inherit from IntegrationTest to make that scaffolding available.
In those cases, your superclass (IntegrationTest, in this case) simply needs to redefine the run method as follows:
class IntegrationTest<Test::Unit::TestCasedef run(*args)returnif@method_name==:default_testsuperendend
The redefinition of run as given above simply makes it so that the test/unit runners do not try to do anything with empty TestCase subclasses that extend IntegrationTest. I could now, for instance, have subclasses of IntegrationTest, without having to define bogus test methods in IntegrationTest.