Fri, 22 Sep 06

Testing against the (Garbage Collection) Clock, or, Don't create your Test Cases as anonymous classes

I wrote a while ago about how I’d started creating my test cases as anonymous classes.

I continued this practice until I was slapped in the face by my own stoopidity yesterday. We were seeing a problem where tests were failing individually but appearing to run fine in rake.

James was looking into it…

James: it seems like the ones defined by Class.new(Test::Unit::TestCase) aren't always being found in the ObjectSpace
James: but I can't quite work out why
James: i'm tempted to name them all as defined classes
Chris: oh ok
Chris: go for it
James: i'm pretty sure that works ok
Chris: interesting
James: only difference i can think of is that if the file is loaded twice you will create two instances of the anonymous class
James: whereas with the named class it will just reopen same class
Chris: why would that be a problem?
James: but it doens't look like its being loaded twice anyway
James: dunno
James: its the only difference i can think of, but it doesn't explain anything
James: can you think of any other differences
Chris: so if you create named classes for the testcases in p_l_i_test does it then fail in rake?
James: yup - and i've seen the problem in other tests where anonymous tests are defined
James: so there are quite a few tests that haven't been running in rake
Chris: bugger
Chris: i think ben just explained the problem
James: yeah?
Chris: as the classes aren't assigned to constants they can be garbage collected
Chris: oops
James: aha
Chris: no more anonymous test classes for christopher
James: that explains - why they were in the ObjectSpace one minute and not the next

Note that p_l_i_test mentioned above is just a shorthand for one of our tests that was failing individually but ‘running’ (or as we now know, not running) from rake.

So, although you get a little added excitement to your testing (“Will this test class run before it gets garbage collected???”), it’s probably a lot safer to not use anonymous classes when testing.

James has just informed me that we gained about 230 tests in rake by naming all of our test cases…