Aric has asked for the wisdom of the Perl Monks concerning the following question:

Hello. Long time listener, first time caller, etc.
I don't want my test script to continue past its first failing test. Is there a way to have the script die upon any failing test, without having to put "or die" after each test? Maybe some construct or argument I can pass to Test::More (I only see 'tests', 'no_plan', and 'skip_all').
Eg.,
ok( 1 == 2 ) or die; ok( $foo eq $bar ) or die; etc...
Thank you.

Replies are listed 'Best First'.
Re: Test Or Die
by JavaFan (Canon) on May 14, 2009 at 14:32 UTC
    sub ok ($;$) { &Test::More::ok(@_) or BAIL_OUT("Test is failing") }
Re: Test Or Die
by moritz (Cardinal) on May 14, 2009 at 14:35 UTC
    Test::Most has the die_on_fail sub which does just that.
Re: Test Or Die
by Arunbear (Prior) on May 14, 2009 at 14:50 UTC
    Or you could use Fatal e.g.
    use strict; use warnings; use Test::More tests => 2; use Fatal qw/ ok /; ok(1 == 2 , '1 is 2'); ok(1 == 1 , '1 is 1');
      Fatal is deprecated, use autodie instead e.g.
      use Test::More qw/no_plan/; use autodie qw/use_ok require_ok ok .../; require_ok('Some::Class'); use_ok('Some::Class'); . . .
      A user level that continues to overstate my experience :-))
        I was referring to the Fatal that is part of the Perl core (not the one included in the autodie distribution) and its documentation doesn't mention anything about it being deprecated.
Re: Test Or Die
by jettero (Monsignor) on May 14, 2009 at 14:32 UTC
    I don't know about Test::More specifically, but I know for sure this works how you want in Test and it appears to work just fine (to me) in Test::More also. Oh, I see, have it do it without or die... I totally missed that. Just write a sub like Javafan does below.

    Although, I'd like to say that it probably doesn't normally make sense to bail on the tests. Unless you can say that no test could possibly fail unless blarg happens. But then why not explicitly skip tests when blarg happens?

    -Paul