TEST2::FORMATTER(3perl)
π Test2::Formatter π
okay so bestie, welcome to the Test2::Formatter page from the Perl Programmers Reference Guide β it's the namespace situation for formatters, and we're gonna get you comfy with it. no cap, it's a short one. π
β¨ NAME β¨
Test2::Formatter β literally just the namespace for formatters. that's the whole vibe. π¦
πΈ DESCRIPTION πΈ
this is the namespace for formatters. it is an empty package. that's it. that's the description. it's giving minimalist. π
π CREATING FORMATTERS π
okay so THIS is the juicy part. a formatter is any package or object with a write($event, $num) method. like, that's the whole job requirement. main character energy fr fr.
package Test2::Formatter::Foo;
use strict;
use warnings;
sub write {
my $self_or_class = shift;
my ($event, $assert_num) = @_;
...
}
sub hide_buffered { 1 }
sub terminate { }
sub finalize { }
sub supports_tables { return $BOOL }
sub new_root {
my $class = shift;
...
$class->new(@_);
}
1;
let's break it down, bestie. π«Ά
the write method is a method (iconic of it), so it either gets a class or an instance. the two arguments are the $event object it should record, and the $assert_num, which is the number of the current assertion (ok), or the last assertion if this event is not itself an assertion. the assertion number may be any integer 0 or greater, and may be undef in some cases. so don't assume, okay? π
the hide_buffered() method must return a boolean. this is used to tell buffered subtests whether or not to send it events as they are being buffered. see "run_subtest(...)" in Test2::API for more information. iykyk. β¨
the terminate and finalize methods are optional methods that you can implement if the format you're generating needs to handle these cases β for example, if you are generating XML and need to close open tags. very "clean up after yourself" of you. π§
the terminate method is called when an event's terminate method returns true, for example when a Test2::Event::Plan has a 'skip_all' plan, or when a Test2::Event::Bail event is sent. the terminate method is passed a single argument: the Test2::Event object which triggered the terminate. π
the finalize method is always the last thing called on the formatter β except when terminate is called for a Bail event. (yes, there's an exception. there's always an exception.) it is passed the following arguments:
the supports_tables method should be true if the formatter supports directly rendering table data from the "info" facets. this is a newer feature and many older formatters may not support it. when not supported, the formatter falls back to rendering "detail" instead of the "table" data. bestie, if your formatter is old, it's just doing its best. πΈ
the new_root method is used when constructing a root formatter. the default is to just delegate to the regular new() method, so most formatters can ignore this entirely. ignore away, queen. π
and okay, here's the actual list of what finalize gets handed, in case you were like "wait, what arguments??" β it's these:
- The number of tests that were planned
- The number of tests actually seen
- The number of tests which failed
- A boolean indicating whether or not the test suite passed
- A boolean indicating whether or not this call is for a subtest
ngl the original page drops this list down here after thesupports_tablesandnew_roottalk, which is a little chaotic of it, but we keep the facts exactly where they were because facts don't move. π
and back to new_root: the new_root method is called when Test2::API::Stack initializes the root hub for the first time. most formatters will simply have this call "$class->new", which is the default behavior. some formatters however may want to take extra action during construction of the root formatter β this is where they can do that. do your little extra thing, you've earned it. β¨
β SOURCE β
the source code repository for Test2 can be found at <https://github.com/Test-More/test-more/>. go star it, it's free. π
π MAINTAINERS π
Chad Granum <exodist@cpan.org> β the icon, the legend. π
πΈ AUTHORS πΈ
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <https://dev.perl.org/licenses/>
okay that's the whole page bestie, you ate it up π
this was Test2::Formatter, page 3perl from the Perl Programmers Reference Guide, and if you're curious it came out of perl v5.44.0, dated 2026-10-17. slay, periodt. πβ¨π¦
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms