[tpm] testing; using eval to catch an error in a module

Adam Prime adam.prime at utoronto.ca
Sat Aug 29 10:11:23 PDT 2009


Madison Kelly wrote:
> Hi all,
> 
>   I've been working on a test script for my module suite/hobby and I've 
> been having a bit of trouble. I want to trap errors in the module 
> without killing the test script. I was hoping I could do something like:
> 
> eval {
>     $an->foo("some Illegal arg");
> }
> if ($@)
> {
>     print "The module 'die'ed as planned and emitted error: [$@]\n";
> }
> else
> {
>     die "Testing of module death failed as [$@] wasn't set.\n";
> }

That seems like it should work, provided that whatever your $an is 
actually die's if it's given an illegal argument.

> 
> I think this is because it's not a compile time error, but the perldoc 
> for eval shows trapping for a divide by zero error, which as I 
> understand it would trigger on execution, not compile.
> 
> Am I missing something fundamental? Should I be approaching my testing 
> in a different way?

It doesn't look like you're using any of the existing perl testing 
stuff, which i'd recommend.  I'm not all that familiar with it myself, 
but i know that michael went to schwern's testing workshop at yapc this 
year, so he might be able to point you to some good starting points.

this seems like it might be a decent start though:

http://books.google.ca/books?id=o9xWJtvhVZ4C&dq=perl+testing


Adam


More information about the toronto-pm mailing list