ajw*_*ood 5 testing perl unit-testing
有没有办法为你期望死的Perl调用编写测试?我想验证某些调用会因格式错误的输入而死亡.
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") );
}
Run Code Online (Sandbox Code Playgroud)
    use Test::Exception;
dies_ok { $foo->method } 'expecting to die';
Run Code Online (Sandbox Code Playgroud)
        您将测试包装在一个eval { ... }块中并检查是否$@已设置.
eval { test_thats_supposed_to_fail() };
ok( $@ , "test failed like it was supposed to" );
Run Code Online (Sandbox Code Playgroud)