Testing Across the Network

     

Test::Harness::Straps makes writing custom test harnesses easy, but it's more flexible than you might think. Its input can come from anywhere . Have you ever wanted to run tests on a remote machine and summarize their output locally? That's no problem.

How do I do that?

Save the following code as network_harness.pl :

 use Net::SSH::Perl;     use Test::Harness::Straps;     my $strap   = Test::Harness::Straps->new(  );     my $ssh     = Net::SSH::Perl->new( 'testbox' );     $ssh->login(qw(  username password  ));     my ($stdout, $stderr, $exit) = $ssh->cmd( 'runtests' );     my %results = $strap->analyze_fh( 'testbox tests', $stdout );     # parse %results as normal 


Note: The first argument to analyze_fh( ) is the test's name, corresponding to the test file name used with analyze_file( ) .

Suppose that you have code running on a machine named testbox . You have access to that machine via SSH, and you have a program on that machine called runtests that knows how to run tests for your application. Run network_harness.pl as a normal Perl program and it will gather and parse the output from testbox , reporting the results.

What just happened ?

The harness connects to the testbox machine through SSH by using the provided username and password. Then it issues the runtests command to the remote machine, collects the results, and passes the output of the command to the TAP parser object. From there, do whatever you like with the results.

What about...

Q:

Does the other machine have to have Perl running?

A:

No, it can use any other language as long as it produces TAP output.

Q:

What if you don't want to or are unable to read from a socket on the remote machine?

A:

Put the test output into an array of lines, perhaps by reading it from a web page on a remote server, and then use the analyze( ) method:

 use LWP::Simple;     use Test::Harness::Straps;     my $strap   = Test::Harness::Straps->new(  );     my $output  = get( 'http://testbox/tests/smoketest.t' );     my @lines   = split( /\n/, $output );     my %results = $strap->analyze( 'testbox smoketest', \@lines );     # parse %results as normal 

The only trick to this example is that analyze( ) expects a reference to an array of lines of test output as its second argument. Otherwise, it behaves exactly as normal.



Perl Testing. A Developer's Notebook
Perl Testing: A Developers Notebook
ISBN: 0596100922
EAN: 2147483647
Year: 2003
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net