table of contents
other versions
- wheezy 5.14.2-21+deb7u3
- jessie 5.20.2-3+deb8u6
- testing 5.24.1-3
- unstable 5.24.1-3
- experimental 5.26.0-1
other sections
TAP::Harness(3perl) | Perl Programmers Reference Guide | TAP::Harness(3perl) |
NAME¶
TAP::Harness - Run test scripts with statisticsVERSION¶
Version 3.23DESCRIPTION¶
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¶
"new"my %args = ( verbosity => 1, lib => [ 'lib', 'blib/lib', 'blib/arch' ], ) my $harness = TAP::Harness->new( \%args );The constructor returns a new "TAP::Harness" object. It accepts an optional hashref whose allowed keys are:
- •
- "verbosity"
1 verbose Print individual test results to STDOUT. 0 normal -1 quiet Suppress some test output (mostly failures while tests are running). -2 really quiet Suppress everything but the tests summary. -3 silent Suppress everything.
- •
- "timer"
- •
- "failures"
- •
- "comments"
- •
- "show_count"
- •
- "normalize"
- •
- "lib"
- •
- "switches"
- •
- "test_args"
test_args => ['foo', 'bar'],
test_args => { my_test => ['foo', 'bar'], other_test => ['baz'], }
- •
- "color"
- •
- "exec"
exec => ['/usr/bin/ruby', '-w']
exec => sub { my ( $harness, $test_file ) = @_; # Let Perl tests run. return undef if $test_file =~ /[.]t$/; return [ qw( /usr/bin/ruby -w ), $test_file ] if $test_file =~ /[.]rb$/; }
- •
- "merge"
- •
- "sources"
sources => { Perl => { exec => '/path/to/custom/perl' }, File => { extensions => [ '.tap', '.txt' ] }, MyCustom => { some => 'config' }, }
- •
- "aggregator_class"
- •
- "version"
- •
- "formatter_class"
- •
- "multiplexer_class"
- •
- "parser_class"
- •
- "scheduler_class"
- •
- "formatter"
- •
- "errors"
errors => 1
- •
- "directives"
- •
- "ignore_exit"
- •
- "jobs"
- •
- "rules"
$harness->rules( { par => [ { seq => '../ext/DB_File/t/*' }, { seq => '../ext/IO_Compress_Zlib/t/*' }, { seq => '../lib/CPANPLUS/*' }, { seq => '../lib/ExtUtils/t/*' }, '*' ] } );
- •
- "stdout"
- •
- "trap"
Instance Methods¶
"runtests"$harness->runtests(@tests);Accepts an 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. It is possible to provide aliases that will be displayed in place of the test name by supplying the test as a reference to an array containing "[ $test, $alias ]":
$harness->runtests( [ 't/foo.t', 'Foo Once' ], [ 't/foo.t', 'Foo Twice' ] );Normally it is an error to attempt to run the same test twice. Aliases allow you to overcome this limitation by giving each run of the test a unique name. 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. "summary"
$harness->summary( $aggregator );Output the summary for a TAP::Parser::Aggregator. "aggregate_tests"
$harness->aggregate_tests( $aggregate, @tests );Run the named tests and display a summary of result. Tests will be run in the order found. Test results will be added to the supplied TAP::Parser::Aggregator. "aggregate_tests" may be called multiple times to run several sets of tests. Multiple "Test::Harness" instances may be used to pass results to a single aggregator so that different parts of a complex test suite may be run using different "TAP::Harness" settings. This is useful, for example, in the case where some tests should run in parallel but others are unsuitable for parallel execution.
my $formatter = TAP::Formatter::Console->new; my $ser_harness = TAP::Harness->new( { formatter => $formatter } ); my $par_harness = TAP::Harness->new( { formatter => $formatter, jobs => 9 } ); my $aggregator = TAP::Parser::Aggregator->new; $aggregator->start(); $ser_harness->aggregate_tests( $aggregator, @ser_tests ); $par_harness->aggregate_tests( $aggregator, @par_tests ); $aggregator->stop(); $formatter->summary($aggregator);Note that for simpler testing requirements it will often be possible to replace the above code with a single call to "runtests". Each element of the @tests array is either:
- •
- the source name of a test to run
- •
- a reference to a [ source name, display name ] array
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.
CONFIGURING¶
"TAP::Harness" is designed to be easy to configure.Plugins¶
"TAP::Parser" plugins let you change the way TAP is input to and output from the parser. TAP::Parser::SourceHandlers handle TAP input. You can configure them and load custom handlers using the "sources" parameter to "new". TAP::Formatters handle TAP output. You can load custom formatters by using the "formatter_class" parameter to "new". To configure a formatter, you currently need to instantiate it outside of TAP::Harness and pass it in with the "formatter" parameter to "new". This may be addressed by adding a formatters parameter to "new" in the future."Module::Build"¶
Module::Build version 0.30 supports "TAP::Harness". To load "TAP::Harness" plugins, you'll need to use the "tap_harness_args" parameter to "new", typically from your "Build.PL". For example:Module::Build->new( module_name => 'MyApp', test_file_exts => [qw(.t .tap .txt)], use_tap_harness => 1, tap_harness_args => { sources => { MyCustom => {}, File => { extensions => ['.tap', '.txt'], }, }, formatter => 'TAP::Formatter::HTML', }, build_requires => { 'Module::Build' => '0.30', 'TAP::Harness' => '3.18', }, )->create_build_script;See "new"
"ExtUtils::MakeMaker"¶
ExtUtils::MakeMaker does not support TAP::Harness out-of-the-box."prove"¶
prove supports "TAP::Harness" plugins, and has a plugin system of its own. See "FORMATTERS" in prove, "SOURCE HANDLERS" in prove and App::Prove for more details.WRITING PLUGINS¶
If you can't configure "TAP::Harness" to do what you want, and you can't find an existing plugin, consider writing one. The two primary use cases supported by TAP::Harness for plugins are input and output:- Customize how TAP gets into the parser
- To do this, you can either extend an existing TAP::Parser::SourceHandler, or write your own. It's a pretty simple API, and they can be loaded and configured using the "sources" parameter to "new".
- Customize how TAP results are output from the parser
- To do this, you can either extend an existing
TAP::Formatter, or write your own. Writing formatters are a bit more
involved than writing a SourceHandler, as you'll need to understand
the TAP::Parser API. A good place to start is by understanding how
"aggregate_tests" works.
SUBCLASSING¶
If you can't configure "TAP::Harness" to do exactly what you want, and writing a plugin isn't an option, consider extending it. It is designed to be (mostly) easy to subclass, though the cases when sub-classing is necessary should be few and far between.Methods¶
The following methods are ones you may wish to override if you want to subclass "TAP::Harness".- "new"
- "runtests"
- "summary"
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::HarnessNote 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::Harness2011-09-19 | perl v5.14.2 |