I wrote a test, then want a bunch more almost like it but with different data. So I put all the guts into reusable sub's and drive the actual test case with a couple calls and only code that's really different for this case.

The code in the sub's will do things like open ... or die. If I change the die to a fail then it doesn't stop trying to go on, when it needs to jump to the next whole test case.

If I put a eval around the top-level test case code, I have more duplication in the "catch" part.

Hmm, I suppose I can always fail (printing the right message) and then die in the common code, but that's less elegant than the "...or die" idiom. It turns into an if block, or more cryptically a do block in the "or" part. If I make a die-ing fail wrapper, I'd need to do similar to every Test::More handy function.

There are lots of ways to write it on one line anyway, and maybe a few ways to reuse the code. How would you do it? I'm interested in generalizing the idiom to work with any of the test primitives, and keeping the code clear and short.

TIMTOWTDI,
—John

#start the test case. my $x= new blahblah; # what I'm testing. # The above and following method calls will be different for each test +. $x->foo (5000); $x->bar(200); # output_test is my common code I'll use i'll use to validate all the +similar tests. my $ot= output_test->new ("out1"); # ready... different name for each +. my $outfile= $ot->openout(); # get set... (common code to generate fi +le name, open it for output, check for errors) $x->emit ($outfile); # this method call will differ for each test cas +e, so it's not part of the common code. $ot->ok_files_match(); # and back to the common code again to compare + the resulting file with a reference "correct" file. # do all that again, with differences. # ...
How should the $ot-> methods contain the common code to "fail" or otherwise validate things as it goes?

P.S. Perhaps I must have a "catch" (if ($@) { ...) for this top-level code that contains the fail, because the code I'm calling (e.g. emit()) might die, and that's not specially-written test code.


In reply to Writing code using Test::More -- how to fail within subs? 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.