views:

41

answers:

1

I have written some Objective-C test cases. A typical looks the following:

- (void) test_size_Equals_Excpectation_After_initWithSize
{
    int size = 10;
    Buffer* buff = [[Buffer alloc] initWithSize:size];

    GHAssertEquals([buff size], size, nil);
}

Buffer alloc returns buffer with reference count 1, I didn't care to call autorelease on buffer, so it is leaked after the funciton exits. Is this acceptable in testing scenarios?

+5  A: 

Doesn't sound like a good idea.

Your test code should be written as well as app code.

It's all code.

Who's to say that leak doesn't have adverse effects on the rest of your tests causing some failures to be reported as successes or vice versa?

Chad
Agreed. Test code won't run persistent (which means the leaks will clean up on exit). On the other hand you are altering the context in which the tests run in subtle ways. Once your tests get more complex (perhaps using some of your setup code in mockups) things get dicey. Just release the buffer like you normally would and avoid bad habits.
Godeke
Most testing frameworks have setup/teardown methods that can help manage memory, depending on your circumstances.
nall