如何在调用 require_ok '*.pl' 时传递参数以通过 Test::More 进行测试

How to pass Arguments when call require_ok '*.pl' to test by Test::More

我想知道如何单独测试 *.pl 文件中的每个子路由。 但不能使用 'require' 子句,因为某些 *.pl 需要参数。

例如

use Test::More;
require "some.pl"

总是会在 'require' 测试失败。
因为 "some.pl " 需要一个参数并以

结尾
exit(0);

文件的。

我只想单独测试 "Func1,usage,...whatever," '*.pl' 中的每个子路由。

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");