Greetings,

I'm in the habit of using or die... in many parts of my code. Lately I've begun to use Test::* tools. Now the trouble with die is that the program exits, never returning to the test harness. In a large group of tests I can't know what test actually failed/died. Consider:

#!/usr/bin/perl use strict; use warnings; use Test::More; ok( _pass(), "Pass" ); ok( _fail(), "Fail" ); done_testing(); sub _pass { return 1 } sub _fail { die "Die, Die, Die"; } neil@ettin ~/src/perltest $ ./foo.pl ok 1 - Pass Die, Die, Die at ./foo.pl line 18. # Tests were run but no plan was declared and done_testing() was not s +een.

I've been using do blocks, but feel clunky.

#!/usr/bin/perl use strict; use warnings; use Test::More; ok( _pass(), "Pass" ); ok( _fail(), "Fail" ); done_testing(); sub _pass { return 1 } sub _fail { 0 or do { warn "Die, Die, Die"; return 0; }; } neil@ettin ~/src/perltest $ ./foo.pl ok 1 - Pass Die, Die, Die at ./foo.pl line 19. not ok 2 - Fail # Failed test 'Fail' # at ./foo.pl line 8. 1..2 # Looks like you failed 1 test of 2.

Is there a better way?

Neil Watson
watson-wilson.ca


In reply to Testing and die by neilwatson

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.