有没有办法为你期望死的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") );
}
答案 0 :(得分:6)
请参阅Test::Exception:
use Test::Exception;
dies_ok { $foo->method } 'expecting to die';
答案 1 :(得分:5)
您将测试包装在eval { ... }
块中,并检查是否设置了$@
。
eval { test_thats_supposed_to_fail() };
ok( $@ , "test failed like it was supposed to" );