Perl Mortality Planning

Is there a way to write tests for Perl calls that you expect to die? I would like to verify that some calls will die with poorly formatted inputs.

sub routine_a {
   my $arg = shift;
   die if $arg eq 'FOO';
   print "routine_a: $arg\n";
}
sub routine_b {
   my $arg = shift;
   die if $arg eq 'BAR';
   print "routine_b: $arg\n";
}

sub test_all {
   assert( routine_a("blah") );
   assert( routine_b("blab") );
   assert_death( routine_a("FOO") );
   assert_death( routine_b("BAR") );
}
+5
source share
2 answers

See Test :: Exception :

use Test::Exception;
dies_ok { $foo->method } 'expecting to die';
+6
source

You complete the test in a block eval { ... }and check if it has been installed $@.

eval { test_thats_supposed_to_fail() };
ok( $@ , "test failed like it was supposed to" );
+5
source

All Articles