You are viewing the version of this documentation from Perl 5.26.1. View the latest version



Test2::Formatter::TAP - Standard TAP formatter


This is what takes events and turns them into TAP.


use Test2::Formatter::TAP;
my $tap = Test2::Formatter::TAP->new();

# Switch to utf8

$tap->write($event, $number); # Output an event


$bool = $tap->no_numbers

Use to turn numbers on and off.

$arrayref = $tap->handles

Can be used to get/set the filehandles. Indexes are identified by the OUT_STD and OUT_ERR constants.

$encoding = $tap->encoding

Get or set the encoding. By default no encoding is set, the original settings of STDOUT and STDERR are used.

This directly modifies the stored filehandles, it does not create new ones.

$tap->write($e, $num)

Write an event to the console.

Test2::Formatter::TAP->register_event($pkg, sub { ... });

In general custom events are not supported. There are however occasions where you might want to write a custom event type that results in TAP output. In order to do this you use the register_event() class method.

package My::Event;
use Test2::Formatter::TAP;

use base 'Test2::Event';
use Test2::Util::HashBase qw/pass name diag note/;

    sub {
        my $self = shift;
        my ($e, $num) = @_;
        return (
            [Test2::Formatter::TAP::OUT_STD, "ok $num - " . $e->name . "\n"],
            [Test2::Formatter::TAP::OUT_ERR, "# " . $e->name . " " . $e->diag . "\n"],
            [Test2::Formatter::TAP::OUT_STD, "# " . $e->name . " " . $e->note . "\n"],



All these methods require the event itself. Optionally they can all except a test number.

All methods return a list of array-refs. Each array-ref will have 2 items, the first is an integer identifying an output handle, the second is a string that should be written to the handle.

@out = $TAP->event_ok($e)
@out = $TAP->event_ok($e, $num)

Process an Test2::Event::Ok event.

@out = $TAP->event_plan($e)
@out = $TAP->event_plan($e, $num)

Process an Test2::Event::Plan event.

@out = $TAP->event_note($e)
@out = $TAP->event_note($e, $num)

Process an Test2::Event::Note event.

@out = $TAP->event_diag($e)
@out = $TAP->event_diag($e, $num)

Process an Test2::Event::Diag event.

@out = $TAP->event_bail($e)
@out = $TAP->event_bail($e, $num)

Process an Test2::Event::Bail event.

@out = $TAP->event_exception($e)
@out = $TAP->event_exception($e, $num)

Process an Test2::Event::Exception event.

@out = $TAP->event_skip($e)
@out = $TAP->event_skip($e, $num)

Process an Test2::Event::Skip event.

@out = $TAP->event_subtest($e)
@out = $TAP->event_subtest($e, $num)

Process an Test2::Event::Subtest event.

@out = $TAP->event_other($e, $num)

Fallback for unregistered event types. It uses the Test2::Event API to convert the event to TAP.


The source code repository for Test2 can be found at


Chad Granum <>


Chad Granum <>
Kent Fredric <>


Copyright 2016 Chad Granum <>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.