From: Eric Hodel Date: 2006-05-03T06:19:37+09:00 Subject: Re: Unit test setup On May 2, 2006, at 1:38 PM, John Wilger wrote: > 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 Eww, yuck. > However, Ruby doesn't have "deconstructors" for classes (AFAIK). Ruby doesn't have deconstructors at all. >> 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. > > [...] > > However, one of the rules of agile unit testing is also that the tests > must be able to run quickly (so that people will actually bother to > run them). Therefore, it is sometimes (though rarely, if you're doing > things right) helpful to make sure expensive operations happen only > when absolutely needed -- but you should only make this compromise if > you can be absolutely sure that you're not introducing state > dependencies into your tests. When I require an expensive setup per test invocation my code is telling me it is not easily testable, so instead of hacking Test::Unit I refactor. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com