I'm looking at Test::More, and there is nothing in there to test that some expression is supposed to die with an exception. Is there some other Testharness-compatible module that helps with this?

I put together an example of an effective test:

my $test_name= "no such bundle"; try { HTML::EntityReference::ordinal('CapitalDifferentialD', ':fooXX'); fail ($test_name); } catch { my $exception= $_; like ($exception, qr/:fooXX/, "$test_name - mentions cause of erro +r"); like ($exception, qr/at t\/tests\.t/, "$test_name - carps correctl +y"); pass ($test_name); } ;
The same name is used in the pass and fail so we know they match. It makes sure the exception branch is taken.

It also performs tests on the "die" string. I'd like to get the full file name and line number of the try'ed expression, but making sure the carp string gives this file is enough.

So if this were canned somehow, I need to specify the code to run, and sub-tests on the exception with suitable labels.

If there is nothing like this already, how have people been testing? How might I reuse this construct so I don't have to duplicate so much for every one I want to check?

Update:

With the suggestion of Test::Exception, my code becomes

my $test_name= "no such bundle"; dies_ok { HTML::EntityReference::ordinal('CapitalDifferentialD', ':foo +XX') } $test_name; like ($@, qr/:fooXX/, "$test_name - mentions cause of error"); like ($@, qr/at t\/tests\.t/, "$test_name - carps correctly");
Saving the contents of the exception string and then testing it after the catch block addressed one of my concerns, with test cases being on a conditional path, and how to get them inside the canned construct.

But its use of $@ makes me worry that this doesn't handle modern (e.g. Moose) code, as with my own recent confusion as to $@ vanishing out from under me. It's fine for my immediate concern though.

Update again:

With the use of Test::Fatal, it becomes:

my $test_name= "no such bundle"; my $err= exception { HTML::EntityReference::ordinal('CapitalDifferenti +alD', ':fooXX') } ; ok (defined $err, $test_name); like ($err, qr/:fooXX/, "$test_name - mentions cause of error"); like ($err, qr/at t\/tests\.t/, "$test_name - carps correctly");
It's actually one more line, since exception doesn't itself report, and I have to save the exception in my own variable. That prevents problems with the ephemeral $@, but it is more noise. I wonder if my own implied global variable, or a localized $_, would be useful for something like that.

I also found that the use of carp within the code still reports the call at test.t, not within the file that contains the exception function. Even though the docs implied that, I guess it is using some other technique to mark the function as "safe" to carp even though it is still on the call stack.

—John


In reply to testing the exception throwing by John M. Dlugosz

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.