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

my $errmsg = eval(SubCall($silly)); ok ("Stop messing around." eq $errmsg);

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.


In reply to Testing error handling that calls "die" by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.