The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

TAP::Harness - Run test scripts with statistics

VERSION

Version 2.99_03

DESCRIPTION

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

SYNOPSIS

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

METHODS

Class Methods

auto_inherit

Defaults to false. Set this class constant to true in your harness subclass to engage in "chained cooperative inheritance." If your subclass declares this, it is a promis to not get in the way of other subclasses -- e.g. it should do things such call SUPER::method() from any overridden methods.

  use base 'TAP::Harness';
  use constant auto_inherit => 1;

inherit

When `prove` needs to utilize multiple harness subclasses, they are built into a chain of "cooperative inheritance" (provided that their auto_inherit() method is true.)

  $class->inherit($base_class);

This allows multiple classes, all of which inherit directly from TAP::Harness, to be stacked:

    package Foo;
    our @ISA = qw( TAP::Harness );
    use constant auto_inherit => 1;

    package Bar;
    our @ISA = qw( TAP::Harness );
    use constant auto_inherit => 1;

    Bar->inherit('Foo');

Would create an inheritance chain like this:

    Bar ISA Foo ISA TAP::Harness

This feature is only a temporary measure to allow experimentation with customizations. The programmer is advised to be aware of which other subclasses are involved and what they do.

new

 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:

  • verbosity

    Set the verbosity level.

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

  • color

    Attempt to produce color output.

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

    The name of the class to use to format output. The default is TAP::Formatter::Console.

  • formatter

    If set formatter must be an object that is capable of formatting the TAP output. See TAP::Formatter::Console for an example.

  • 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 filehandle for catching standard output.

Any keys for which the value is undef will be ignored.

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

aggregate_tests

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

Tests will be run in the order found.

jobs

Returns the number of concurrent test runs the harness is handling. For the default harness this value is always 1. A parallel harness such as TAP::Harness::Parallel will override this to return the number of jobs it is handling.

fork

If true the harness will attempt to fork and run the parser for each test in a separate process. Currently this option requires Parallel::Iterator to be installed.

SUBCLASSING

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.

Methods

TODO: This is out of date

The following methods are ones you may wish to override if you want to subclass TAP::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' ));
  • 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};
    
     for 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.

make_parser

Make a new parser and display formatter session. Typically used and/or overridden in subclasses.

    my ( $parser, $session ) = $harness->make_parser;

finish_parser

Terminate use of a parser. Typically used and/or overridden in subclasses. The parser isn't destroyed as a result of this.

REPLACING

If you like the prove 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 prove utility like so:

 prove --harness My::Test::Harness

Note that while prove 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.

SEE ALSO

Test::Harness