++ed by:

1 non-PAUSE user.

Andy Armstrong


TAP::Harness - Run Perl test scripts with statistics


Version 0.54


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


 use TAP::Harness;
 my $harness = TAP::Harness->new( \%args );


Class Methods


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

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

  • verbose

    Print individual test results to STDOUT.

  • timer

    Append run time for each test to output. Uses Time::HiRes if available.

  • failures

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

  • 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.

  • 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'
  • merge

    If merge is true the harness will create parsers that merge STDOUT and STDERR together for any processes they start.

  • formatter

    If set formatter must be an object that is capable of formatting individual items from the TAP stream. For each type of item it is capable of formatting it must expose a method called format_type.

    For example:

        sub format_yaml {
            my ($self, $harness, $result, $prev_result) = @_;
            # Format the item and return a string
            return _format_yaml_line( $result, $prev_result );

    The formatting method is called with three arguments in addition to $self:


    The test harness.


    The result which we should format.


    The previous result. This is necessary in the case of, for example, format_yaml which will want to know whether the preceding test passed or failed.

  • 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
  • directives

    If set to a true value, only test results with directives will be displayed. This overrides other settings such as verbose or failures.

  • stdout

    A scalar reference (experimental) for catching standard output. Maybe should be a filehandle.

Instance Methods



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 TAP::Parser::new() as a source. See TAP::Parser for more information.

Tests will be run in the order found.

If the environment variable PERL_TEST_HARNESS_DUMP_TAP is defined it should name a directory into which a copy of the raw TAP for each test will be written. TAP is written to files named for each test. Subdirectories will be created as needed.

Returns a TAP::Parser::Aggregator containing the test results.


  $harness->aggregate_tests( $aggregate, @tests );

Tests will be run in the order found.


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


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


  $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' ));
  • tests

    This is an array reference of all test names. To get the TAP::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.



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



Identical to output, this method is called for any output which represents a failure.


 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.


 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, 9-10



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


If you like the runtests utility and TAP::Parser but you want your own harness, all you need to do is write one and provide new and runtests methods. Then you can use the runtests utility like so:

 runtests --harness My::Test::Harness

Note that while runtests accepts a list of tests (or things to be tested), new has a fairly rich set of arguments. You'll probably want to read over this code carefully to see how all of them are being used.