从主驱动程序脚本perl调用测试脚本

时间:2014-06-06 09:28:42

标签: perl

我有一个主设置脚本,它设置测试环境并将数据存储在一些变量中:

package main;
use Test::Harness;

our $foo, $xyz, $pqr;
($foo, $xyz, $pqr) = &subroutinesetup();
# ^ here

@test_files = glob "t/*";
print "Executing test @test\n";
runtests(@test_files);

在测试文件夹中,我有一个测试套件(t / testsuite1.t,testsuite2.t等)。 如何在testsuite1.t中访问$ foo的值?

package main;
use Test::More;
$actual = getActual();
is($foo, $actual, passfoor);
#   ^ here

done_testing();

2 个答案:

答案 0 :(得分:1)

使用Storable在第一个脚本中存储数据并从其他脚本中检索数据。

main.pl

  ($foo, $xyz, $pqr) = &subroutinesetup();
  store ($foo, "/home/chankey/testsuite.$$") or die "could not store";
  system("perl", "testsuite.pl", $$) == 0 or die "error";

testsuite.pl

my $parentpid = shift;
my $ref = retrieve("/home/chankey/testsuite.$parentpid") or die "couldn't retrieve";
print Dumper $ref;

您已收到$foo中的$ref。现在按照你想要的方式使用它。

答案 1 :(得分:1)

您不能直接共享变量,因为为每个测试文件启动了一个新的Perl进程。

Test::Harness文档中所述,您应切换到TAP::Harness。它更灵活:例如,它提供了test_args机制来将参数传递给测试脚本。

$ cat 1.pl
#!/usr/bin/perl
use warnings;
use strict;

use TAP::Harness;

my $harness = 'TAP::Harness'->new({
                  test_args => [ qw( propagate secret ) ]
              });

$harness->runtests('1.t');
__END__

$ cat 1.t
#!/usr/bin/perl
use warnings;
use strict;

use Test::More;

my %args = @ARGV;
is($args{propagate}, 'secret', 'propagated');
done_testing();
相关问题