我想知道如何单独测试*.pl文件中的每个子路由。但是不能使用'require‘子句,因为有些*.pl需要参数。
例如
use Test::More;
require "some.pl"
总是在“需要”时不通过测试。
因为"some.pl“需要一个参数并以
exit(0);
文件里的。
我只想单独测试"Func1,just,...whatever“中的每一个子路由。
some.pl是这样的
my ( $cmd) = @ARGV;
if (!defined $cmd ) {
usage();
} else {
&Func1;
}
exit(0);
sub Func1 {
print "hello";
}
sub usage {
print "Usage:\n",
}
如何通过" test ::More“为”subFunc1“编写测试代码?
任何建议都很感激。
发布于 2020-01-07 05:13:38
要执行您希望退出的独立脚本,请使用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");
https://stackoverflow.com/questions/59621346
复制相似问题