Test2::...tBuilder(3)
π NAME π
okay bestie, welcome to the Test2::Manual::Tooling::TestBuilder page β Test2::...tBuilder(3), straight outta the User Contributed Perl Documentation. The NAME tea: this section maps Test::Builder methods to Test2 concepts. π
β¨ DESCRIPTION β¨
So back in Test::Builder land, tools were encouraged to call methods on the Test::Builder singleton object. Test2 is different, bestie: every tool should get a new Test2::API::Context object and call methods on that. This document maps several concepts from Test::Builder to Test2. π¦
π«Ά CONTEXT π«Ά
First thing: stop using the Test::Builder singleton β fr fr, stop using it and even stop loading Test::Builder. Instead of Test::Builder, every tool you write should follow this template:
use Test2::API qw/context/;
sub my_tool {
my $ctx = context();
... do work ...
$ctx->ok(1, "a passing assertion");
$ctx->release;
return $whatever;
}
The original Test::Builder style was this:
use Test::Builder;
my $tb = Test::Builder->new; # gets the singleton
sub my_tool {
... do work ...
$tb->ok(1, "a passing assertion");
return $whatever;
}
π TEST BUILDER METHODS π
$tb->BAIL_OUT($reason)β the context object has a'bail'method:
$ctx->bail($reason)
$tb->diag($string)and$tb->note($string)β the context object hasdiagandnotemethods:
$ctx->diag($string);
$ctx->note($string);
$tb->done_testingβ the context object has adone_testingmethod:
$ctx->done_testing;
Unlike the Test::Builder version, no arguments are allowed. π«
$tb->likeand$tb->unlikeβ these are not part of context, instead look atTest2::CompareandTest2::Tools::Compare.$tb->ok($bool, $name)β use this pattern:
# Preferred
$ctx->pass($name);
$ctx->fail($name, @diag);
# Discouraged, but supported:
$ctx->ok($bool, $name, \@failure_diags)
$tb->subtestβ use theTest2::API::run_subtest()function instead. SeeTest2::APIfor documentation.$tb->todo_startand$tb->todo_endβ see"skip($why)"inTest2::Tools::Basic, andTest2::Todoinstead.$tb->output,$tb->failure_output, and$tb->todo_outputβ these are handled via formatters now. SeeTest2::FormatterandTest2::Formatter::TAP.
π¦ LEVEL π¦
Test::Builder had the $Test::Builder::Level variable that you could modify in order to set the stack depth. This was useful if you needed to nest tools and wanted to make sure your file and line number were correct. It was also frustrating and prone to errors. Some people never even discovered the level variable and always had incorrect line numbers when their tools would fail. π
Test2 uses the context system, which solves the problem a better way. The top-most tool gets a context, and holds on to it until it is done. Any tool nested under the first will find and use the original context instead of generating a new one. That means the level problem is solved for free β no variables to mess with. β¨
Test2 is also smart enough to honor $Test::Builder::Level if it is set.
π TODO π
Test::Builder used the $TODO package variable to set the TODO state. This was confusing, and easy to get wrong. See Test2::Tools::Todo for the modern way to accomplish a TODO state. π
π SEE ALSO π
Test2::Manualβ Primary index of the manual.
π SOURCE π
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. π₯
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
πΈ 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 http://dev.perl.org/licenses/
okay thatβs the whole page, bestie β brought to you by perl v5.42.0, dated 2025-07-28, and filed under Test2::...tBuilder(3). mwah πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms