The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

TAPx::Harness - Run Perl test scripts with statistics

VERSION

Version 0.50_01

DESCRIPTION

This is a simple test harness which allows tests to be run and results automatically aggregated and output to STDOUT.

SYNOPSIS

 use TAPx::Harness;
 my $harness = TAPx::Harness->new( \%args );
 $harness->runtests(@tests);

METHODS

Class methods

new

 my %args = (
    verbose => 1,
    lib     => [ 'lib', 'blib/lib' ],
 )
 my $harness = TAPx::Harness->new( \%args );

The constructor returns a new TAPx::Harness object. It accepts an optional hashref whose allowed keys are:

  • verbose

    Print individual test results to STDOUT.

  • failures

    Only show test failures (this is a no-op if verbose is selected).

  • merge

    Merges STDOUT and STDERR. This is a highly experimental feature. See TAPx::Parser::Source::Perl::synch_output for more information. Only works for tests using Test::Builder.

  • lib

    Accepts a scalar value or array ref of scalar values indicating which paths to allowed libraries should be included if Perl tests are executed. Naturally, this only makes sense in the context of tests written in Perl.

  • switches

    Accepts a scalar value or array ref of scalar values indicating which switches should be included if Perl tests are executed. Naturally, this only makes sense in the context of tests written in Perl.

  • quiet

    Suppress some test output (mostly failures while tests are running).

  • really_quiet

    Suppress everything but the tests summary.

    Note: if the merge option does not have a true value, failed test information and diagnostics may still be output with really_quiet because of how Test::Builder works (assumes your tests are using Test::Builder.

  • exec

    Typically, Perl tests are run through this. However, anything which spits out TAP is fine. You can use this argument to specify the name of the program (and optional switches) to run your tests with:

      exec => '/usr/bin/ruby -w'
  • execrc

    Location of 'execrc' file. See "USING EXECRC" below.

  • errors

    If parse errors are found in the TAP output, a note of this will be made in the summary report. To see all of the parse errors, set this argument to true:

      errors => 1

Instance Methods

runtests

  $harness->runtests(@tests);

Accepts and array of @tests to be run. This should generally be the names of test files, but this is not required. Each element in @tests will be passed to TAPx::Parser::new() as a source. See TAPx::Parser for more information.

Tests will be run in the order found.

SUBCLASSING

TAPx::Harness is designed to be (mostly) easy to subclass. If you don't like how a particular feature functions, just override the desired methods.

Methods

The following methods are one's you may wish to override if you want to subclass TAPx::Harness.

summary

  $harness->summary( \%args );

summary prints the summary report after all tests are run. The argument is a hashref with the following keys:

  • start

    This is created with Benchmark->new and it the time the tests started. You can print a useful summary time, if desired, with:

      $self->output(timestr( timediff( Benchmark->new, $start_time ), 'nop' ));
  • aggregate

    This is the TAPx::Parser::Aggregate object for all of the tests run.

  • tests

    This is an array reference of all test names. To get the TAPx::Parser object for individual tests:

     my $aggregate = $args->{aggregate};
     my $tests     = $args->{tests};
    
     foreach my $name ( @$tests ) {
         my ($parser) = $aggregate->parsers($test);
         ... do something with $parser
     }

    This is a bit clunky and will be cleaned up in a later release.

output

  $harness->output(@list_of_strings_to_output);

All output from TAPx::Harness is driven through this method. If you would like to redirect output somewhere else, just override this method.

balanced_range

 my @ranges = $harness->balanced_range( $limit, @numbers );

Given a limit in the number of characters and a list of numbers, this method first creates a range of numbers with range and then groups them into individual strings which are roughly the length of $limit. Returns an array of strings.

range

 my @range = $harness->range(@list_of_numbers);

Taks a list of numbers, sorts them, and returns a list of ranged strings:

 print join ', ' $harness->range( 2, 7, 1, 3, 10, 9  );
 # 1-3, 7, 10-9

output_test_failure

  $harness->output_test_failure($parser);

As individual test programs are run, if a test program fails, this method is called to spit out the list of failed tests.

USING EXECRC

Sometimes you want to use different executables to run different tests. If that's the case, you'll need to create an execrc file. The format looks like the following:

 '/usr/bin/perl -wT' => '*'   # default for all programs

 # case-by-case handling

 '/usr/bin/perl -w' => 't/not_taint_safe.t'
 '/usr/bin/ruby -w' => 't/test_is_written_in_ruby.t'

 # drive the argument through a different program:
 '/usr/bin/perl test_html.pl' => 'http://www.google.com/'

The left argument (LHS) is a command for executing and the right side (RHS) must be the name of what is being tested.

If the RHS is '*', then the RHS is the default for any argument not listed as an LHS.

Both the LHS and RHS must be quoted (single or double quotes).

Blank lines are allowed. Lines beginning with a '#' are comments (the '#' may have spaces in front of it). Comments are allowed after the RHS.

So for the above execrc file, if it's named 'my_execrc' (as it is in the examples/ directory which comes with this distribution), then you could potentially run it like this, if you're using the runtests utility:

 runtests --execrc my_execrc t/ - < list_of_urls.txt

Then for a test named t/test_is_written_in_ruby.t, it will be executed with:

 /usr/bin/ruby -w t/test_is_written_in_ruby.t

If the list of urls contains "http://www.google.com/", it will be executed as follows:

 /usr/bin/perl test_html.pl http://www.google.com/

Of course, if test_html.pl outputs anything other than TAP, this will fail.

See the README in the examples directory for a ready-to-run example.