in reply to Writing code using Test::More -- how to fail within subs?

Another way :-)

If you don't mind running an indeterminate number of tests, just use Test::Exception's lives_ok. For example:

use Test::More 'no_plan'; use Test::Exception; lives_ok { my $x= new blahblah; $x->foo (5000); $x->bar(200); my $ot= output_test->new ("out1"); my $outfile= $ot->openout(); $x->emit ($outfile); $ot->ok_files_match(); } 'test case out1 worked'; # ... etc ...

Replies are listed 'Best First'.
Re: Re: Writing code using Test::More -- how to fail within subs?
by John M. Dlugosz (Monsignor) on Feb 09, 2003 at 04:55 UTC
    I think that's close to what I did:
    sub tryit (&$) { my ($code, $label)= @_; eval { &$code }; if ($@) { my $error= $@; # remember that in case fail() changes it. fail $label; diag "$label fails:", $error; } }
    My code ends in an ok...() type statement, so if it reaches the end the tryit wrapper doesn't need to do anything. But if there is an exception, then the wrapper issues the NOT OK line. So, I don't get two tests out on success or one on failure like you seem to be illustrating re 'no_plan'.

    —John