neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing and die
by choroba (Cardinal) on Aug 17, 2014 at 19:54 UTC | |
|
Re: Testing and die
by davies (Monsignor) on Aug 17, 2014 at 21:15 UTC |