我想知道单独测试* .pl文件中每个子路由的方法。但是不能使用'require'子句,因为某些* .pl需要参数。
例如
use Test::More;
require "some.pl"
将始终在“需要”时失败测试。因为“ some.pl”需要一个参数并以
结尾exit(0);
文件的。
我只想分别测试“ * .pl”中的每个子路由“ Func1,使用情况,...无论如何”。
some.pl就是这样
my ( $cmd) = @ARGV;
if (!defined $cmd ) {
usage();
} else {
&Func1;
}
exit(0);
sub Func1 {
print "hello";
}
sub usage {
print "Usage:\n",
}
如何通过“ Test :: More”为“ sub Func1”编写测试代码?
任何建议,谢谢。
要执行您希望退出的独立脚本,请使用system
运行它。捕获输出并在system
调用结束时对其进行检查。
use Test::More;
my $c = system("$^X some.pl arg1 arg2 > file1 2> file2");
ok($c == 0, 'program exited with successful exit code');
open my $fh, "<", "file1";
my $data1 = do { local $/; <$fh> };
close $fh;
open $fh, "<", "file2";
my $data2 = do { local $/; <$fh> };
close $fh;
ok( $data1 =~ /Funct1 output/, "program called Funct1");
ok( $data2 !~ /This is how you use the program, you moron/,
"usage message not printed to STDERR" );
unlink("file1","file2");