Perl-Sensitive Sunglasses | |
PerlMonks |
Testing error handling that calls "die"by davies (Prior) |
on Dec 26, 2011 at 18:19 UTC ( [id://945118]=perlquestion: print w/replies, xml ) | Need Help?? |
davies has asked for the wisdom of the Perl Monks concerning the following question: When users are allowed to provide data to code, it can be taken for granted that they will louse up at some point. I'm trying to write code that will respond with an error message telling users that I can't proceed with the nonsense they have provided. The actual code looks something like if ($silly) {die "Stop messing around."}. This works fine, although the error message is more explicit. However, I want to test it, and the problem is that when I send the invalid parameters from the test module, it causes the test module to die. This isn't fine. I thought that I could trap the death with
While I get the correct error message sent to the console, processing stops. PerlDoc talks about skipping tests, but that's not what I want to do - I actually want to send dud parameters and check that my code degrades gracefully. Test::Most has a lot of information on die, but the docs are very terse and seem to me to be about getting Test::Most to die, rather than about handling expected deaths. Is what I am trying to do possible? Regards, John Davies Update: I tried the two solutions and found the same problem with the eval/$@/like solution as I had with my original code, namely that the test module died. But I didn't look very far as the Test::Exception solution worked fine. It didn't allow me to check that I was getting the right error message, but that's a minor detail. Tests pass and fail when I expect them to. Thank you both.
Back to
Seekers of Perl Wisdom
|
|