Error.pms try/catch gotcha in unit tests

jk2addict on 2005-07-21T23:58:48

I'm such and idiot. I can't believe I never noticed this before. It's quite a dangerous oversight too since it meant tests didn't register a pass or a fail; no output at all which threw off the test counts. Don't let this happen to you.

---some.t---
use Test::More tests => 2;
use Error ':try';

use_ok('MyModule');

try {
   MyModule->foo('badparam');
} catch with MyModule::Exception {
    pass;
} otherwise {
    fail;
};

Of course, if the module throws the exception, we pass. If it throws another exception (or die), we fail. But, if the module unexpectedly succeeds, we neither fail nor pass. That's not so bad right? THe count mismatch would cause the test to fail right? Sure, but picture this:

---some.t---
use Test::More tests => 2;
use Error ':try';

use_ok('MyModule');

try {
   MyModule->foo('badparam');
} catch with MyModule::Exception {
    pass;
} otherwise {
    fail;
};

ok('added this test later in this huge test file');

In this scenerio, you've added a test but forgot to update the test plan count. Now you've declared 2 tests and passed 2 tests. No failures and you never know a completely failing test...didn't. This is ever more true if you're using no_plan. Probably another reason not to ever do that. :-)

Ths answer of course is to put a fail in the try block. Not something that seams obvious upon first thought.

---some.t---
use Test::More tests => 2;
use Error ':try';

use_ok('MyModule');

try {
   MyModule->foo('badparam');

    fail;
} catch with MyModule::Exception {
    pass;
} otherwise {
    fail;
};

Boy do I have a lot of test files to double check. That was a painful lesson. For some reason, part of me expected otherwise to get called when no exception was thrown, but that's silly in retrospect.


another gotcha

perrin on 2005-07-22T04:36:51

Your sample code is missing closing semi-colons on the try() calls. I'm guessing your real code doesn't have this problem.

Re:another gotcha

jk2addict on 2005-07-22T12:42:37

Correct. That's a typo for this post. I'm a ; over-user at heart, so they all had the sem-colons in the real code.

Just out of curiousity, why is that a problem with try in Error?

Re:another gotcha

perrin on 2005-07-22T17:01:56

The try() is just a subroutine call, so it needs a ; at the end of the line like anything else. This is yet another problem with the try/catch syntax in Error.pm.

Re:

Aristotle on 2005-07-22T09:19:44

I believe you want to take a look at Test::Exception.

Re:

jk2addict on 2005-07-22T12:48:11

I was thinking about using Test::Exception way back when, but I decided against it. I didn't want to skip all of those tests everywhere just because it wasn't installed; especially when I could do mostly the same ting in a simple try statement.

Re:

jk2addict on 2005-07-22T12:52:34

To add to that, Err is already a PREREQ for the dist.