我目前正在学习单元测试。为此,我正在写一个脚本,并为整个脚本创建单元测试。到目前为止事情进展顺利,但我试图测试从命令行输入脚本的不正确数据触发帮助信息。如何用Try :: Tiny“捕捉”Perl脚本中的pod2usage出口?
我的代码看起来是这样的:
sub getContext{
my ($help) = @_;
GetOptions(
help|h => \$help,
...
pod2usage if $help;
...
}
我的测试看起来像:
my $help_exception = 0;
try{
getContext({help => 0});
}catch{
$help_exception = 1;
}
ok($help_exception, "Script died correctly when given help flag");
我的输出看起来非常相似:
1..4
ok 1 - use scripts::scriptname;
ok 2
ok 3
# Looks like you planned 4 tests but ran 3.
# Looks like your test exited with 1 just after 3.
帮助标志的测试是测试4,它看起来像我的脚本正在退出而不触发Try :: Tiny try catch块。有没有办法解决这个问题,还是应该以不同的方式写我的测试?
非常感谢你的回应,你会如何本地化CORE :: GLOBAL :: EXIT覆盖? – Moses
使用“本地”内置。 – rafl