Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jk2addict (4946)

jk2addict
  (email not shown publicly)
http://today.icantfocus.com/blog/
AOL IM: chrislaco (Add Buddy, Send Message)
Yahoo! ID: chrislaco@sbcglobal.net (Add User, Send Message)
Jabber: laco@scribblewerks.com

Journal of jk2addict (4946)

Thursday July 21, 2005
06:58 PM

Error.pms try/catch gotcha in unit tests

[ #25838 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Your sample code is missing closing semi-colons on the try() calls. I'm guessing your real code doesn't have this problem.
    • 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?
      • 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.
  • I believe you want to take a look at Test::Exception [cpan.org].
    • 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.