user3714347
user3714347

Reputation: 37

call test scripts from main driver script perl

I have a main setup script which sets up the test env and stores data in some variables:

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

In the test folder I have a testsuite (t/testsuite1.t, testsuite2.t etc.). How can I access the value of $foo inside the testsuite1.t?

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

done_testing();

Upvotes: 1

Views: 84

Answers (2)

choroba
choroba

Reputation: 241988

You can't share a variable directly, because a new Perl process is started for each test file.

As noted in the documentation of Test::Harness, you should switch to TAP::Harness. It's more flexible: for example, it provides the test_args mechanism to pass arguments to test scripts.

$ 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();

Upvotes: 1

Chankey Pathak
Chankey Pathak

Reputation: 21676

Use Storable to store data in first script and retrieve it from other.

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;

You've received the $foo in $ref. Now use it the way you want.

Upvotes: 1

Related Questions