[Chicago-talk] perl tests with overhead

Greg Fast gdf at speakeasy.net
Tue Jul 26 09:37:44 PDT 2005

On Tue, Jul 26, 2005 at 09:57:57AM -0500, JT Smith wrote:
> The problem comes in that I don't want each test to have to seperately
> establish a session before running if I can help it. What I'd like to do
> is create a session and then pass each test a reference to the session.

I recently hacked together a solution to a similar testing problem,
using the previously-unknown-to-me Test::Harness::Straps.  This is the
chunk of Harness that runs the test scripts and analyzes/summarizes
the output ("1..251", "ok 1", etc).

I overrode Straps and swapped out the execution of "perl t/$n.t" with
my own test driver.  This allowed me to factor a large chunk of common
code out of a test suite (basically: configure a server, fork a
server, configure a client, run a client, join and cleanup).

The relevant bits:

  package MyStraps;
  use base 'Test::Harness::Straps';

  sub analyze_file {
    my $self = shift;
    my $case_file = shift;
    # run "<driver> <case_file>"
    my $cmd = $self->_command_line( $self->driver_cmd ) . " $case_file";
    open( my $fh, "$cmd |" ) || die $!;
    return $self->analyze_fh( $case_file, $fh );


  use Test::Harness;
  $Test::Harness::Strap = MyStraps->new();
  runtests( @cases ); # t/1.t, t/2.t, ...

So this lets you do arbitrary work for each test case.  In your
example, you could easily replace the exec of the test command ("perl
t/1.t") with a in-process execution that provides a shared session
(Straps has an analyze() method that takes plain text input), or you
could write a driver script that loads and stores the session before
providing it to an exec'd script (to avoid local pollution).

Normally when you do a "make test", it basically runs runtests().  I
haven't bothered to hack up Makefile.PL to make it use my straps
(because this is an integration test suite that requires external
setup), but it should be possible.

Greg Fast
gdf at speakeasy.net

More information about the Chicago-talk mailing list