From: John Wilger Date: 2006-05-03T05:38:22+09:00 Subject: Re: Unit test setup On 5/2/06, Eric Hodel wrote:> On May 2, 2006, at 6:35 AM, Eust叩quio Rangel wrote:> > Is there a way for use> > general "constructor" and "destructor" methods, to, for example,> > open a socket on the "constructor", execute all the tests and close> > it on the "destructor" > There isn't. Only half correct. You can override the #initialize method in the test case: class TestTheTestsTest < Test::Unit::TestCase def initialize( *args ) super @foo = :bar end def test_initialize assert_equal :bar, @foo end end However, Ruby doesn't have "deconstructors" for classes (AFAIK). > You don't need it and you don't want it. While I would agree with this in almost 100% of cases, there _are_edge cases where this may not be true. The reason you probably don't want it is that you want each test torun in a 'clean' environment. Basically, you want to make sure you'renot introducing dependencies between the tests within a test case. Ifyou do, it makes refactoring that much harder. Also, there's noguarantee about the order in which the individual tests are run withina test case. (I believe they are run in lexical order with the currentTest::Unit implementation, but that's a coincedence, not arequirement.) However, one of the rules of agile unit testing is also that the testsmust be able to run quickly (so that people will actually bother torun them). Therefore, it is sometimes (though rarely, if you're doingthings right) helpful to make sure expensive operations happen onlywhen absolutely needed -- but you should only make this compromise ifyou can be absolutely sure that you're not introducing statedependencies into your tests. In the original example given by Eust叩quio, I would bet that Eric iscorrect -- you probably don't want to do this. --Regards,John Wilgerhttp://johnwilger.com -----------Alice came to a fork in the road. "Which road do I take?" she asked."Where do you want to go?" responded the Cheshire cat."I don't know," Alice answered."Then," said the cat, "it doesn't matter."- Lewis Carrol, Alice in Wonderland