Test2::V1(3perl)
π NAME π
Test2::V1 β V1 edition of the Test2 recommended bundle. π (yes bestie, that's the Test2::V1(3perl) page you just opened, welcome)
β¨ DESCRIPTION β¨
okay so. This is the first sequel to Test2::V0. And like, for new tests? This one is the recommended one over Test2::V0. Periodt. π«Ά
π¦ Key differences from Test2::V0
- Only 1 export by default:
T2() - No pragmas by default
srandandutf8are not enabled by default- Easy to still import everything
- East to still enable pragmas
π NAMING, USING, DEPENDING π
This bundle should not change in a severely incompatible way. Some minor breaking changes, specially bugfixes, may be allowed. But if breaking changes are actually needed, then a new Test2::V#" module should be released instead. Like, ship a whole new letter, don't just jump-scare the existing one. π
Adding new optional exports, and new methods on the T2() handle, are not considered breaking changes, and are allowed without bumping the V# number. Adding new plugin shortcuts is also allowed, but they cannot be added to the -P or -plugins shortcuts without a bump in V# number. No sneaky additions to the shortcut list, iykyk. π
As new V#" modules get released, old ones may be moved to different cpan distributions. So you should always use a specific bundle version and list that version in your distribution's testing requirements. You should never simply list Test2::Suite as your module's dep β instead list the specific bundle, or the tools and plugins you use directly, in your metadata. fr fr, pin your stuff. π§
See the "JUSTIFICATION" section for an explanation of why Test2::V1 was created.
πΈ SYNOPSIS πΈ
π RECOMMENDED
use Test2::V1 -utf8;
T2->ok(1, "pass");
T2->is({1 => 1}, {1 => 1}, "Structures Match");
# Note that prototypes do not work in method form:
my @foo = (1, 2, 3);
T2->is(scalar(@foo), 3, "Needed to force scalar context");
T2->done_testing;
π WORK LIKE V0 DID
use Test2::V1 -ipP;
ok(1, "pass");
is({1 => 1}, {1 => 1}, "Structures Match");
my @foo = (1, 2, 3);
is(@foo, 3, "Prototype forces @foo into scalar context");
# You still have access to T2
T2->ok(1, "Another Pass");
done_testing;
The -ipP argument is short for -include, -pragmas, -plugins which together enable all pragmas, plugins, and import all symbols. It's giving "give me everything" energy and honestly? valid.
Note: The order in whichi,p, andPappear is not important;-Ppiand-piPand any other order are all perfectly valid. π
π IMPORT ARGUMENT GUIDE π
-Por-pluginsβ shortcut to include the following plugins:Test2::Plugin::UTF8,Test2::Plugin::SRand,Test2::Plugin::ExitSummary.-por-pragmasβ shortcut to enable the following pragmas:strict,warnings.-ior-importβ shortcut to import all possible exports.-xβ shortcut to import any sub that has&in its prototype, things likedies { ... },warns { ... }, etc.
While these can be used in method form β T2->dies(sub { ... }) β it is a little less convenient than having them imported. -x will import all of these, and any added in the future or included via an -include => ... import argument.
-ipP,-pPi,-pP,-Pix, etc.. β thei,p,P, andxshort options may all be grouped in any order following a single dash. Mix and match, bestie. π@EXPORT_LISTβ any arguments provided that are not prefixed with a-will be assumed to be export requests. If there is an exported sub by the given name it will be imported into your namespace. If there is no such sub, an exception will be thrown. π!EXPORT_NAMEβ you can prefix an export name with!to exclude it at import time. This is really only usedul when combined with-importor-i.EXPORT_NAME => { -as => "ALT_NAME" }EXPORT_NAME => { -prefix => "PREFIX_" }EXPORT_NAME => { -postfix => "_POSTFIX" }
You may specify a hashref after an export name to rename it, or add a prefix/postfix to the name. Rename your faves, no cap. β¨
π«Ά RENAMING IMPORTS π«Ά
use Test2::V1 '-import', '!ok', ok => {-as => 'my_ok'};
Explanation:
-importβ bring in ALL imports, no need to list them all by hand.!okβ do not importok()(remove it from the list added by-import)ok => {-as => 'my_ok'}β actually, go ahead and importok()but under the namemy_ok().
If you did not add the '!ok' argument then you would have both ok() and my_ok(). Choices, choices. π
π PRAGMAS AND PLUGINS π
NO PRAGMAS ARE ENABLED BY DEFAULT ONLY THE EXIT SUMMARY PLUGIN IS STILL ENABLED BY DEFAULT
This is a significant departure from Test2::V0. bestie it really said "figure it out yourself" and honestly? iconic.
You can enable all of these with the -pP argument, which is short for -plugins, -pragmas. P is short for plugins, and p is short for pragmas. When using the single-letter form they may both be together following a single dash, and can be in any order. They may also be combined with i to bring in all imports. -p or -P ont heir own are also perfectly valid.
π strict
You can enable this with any of these arguments: -strict, -p, -pragmas.
This enables strict for you.
πΈ warnings
You can enable this with any of these arguments: -warnings, -p, -pragmas.
This enables warnings for you.
β srand
You can enable this in multiple ways:
use Test2::V1 -srand
use Test2::V1 -P
use Test2::V1 -plugins
See Test2::Plugin::SRand.
This will set the random seed to today's date. π
You can also set a random seed:
use Test2::V1 -srand => { seed => 'my seed' };
π¦ utf8
You can enable this in multiple ways:
use Test2::V1 -utf8
use Test2::V1 -P
use Test2::V1 -plugins
See Test2::Plugin::UTF8.
This will set the file, and all output handles (including formatter handles), to utf8. This will turn on the utf8 pragma for the current scope.
π summary
This is turned on by default.
You can avoid enabling it at import this way:
use Test2::V1 -summary => 0;
See Test2::Plugin::ExitSummary.
This plugin has no configuration. It just vibes. π
π± ENVIRONMENT VARIABLES π±
See Test2::Env for a list of meaningful environment variables.
π API FUNCTIONS π
See Test2::API for these:
$ctx = T2->context()$events = T2->intercept(sub { ... });
π THE T2() HANDLE π
The T2() subroutine imported into your namespace returns an instance of Test2::V1::Handle. This gives you a handle on all the tools included by default. It also creates a completely new namespace for use by your test that can have additional tools added to it. main character namespace energy, no cap. β¨
π ADDING/OVERRIDING TOOLS IN YOUR T2 HANDLE π
# Method 1
use Test2::V1 T2 => {
include => [
['Test2::Tools::MyTool', 'my_tool', 'my_other_tool'],
['Data::Dumper', 'Dumper'],
],
};
# Method 2
use Test2::V1 T2 => {
include => {
'Test2::Tools::MyTool' => ['my_tool', 'my_other_tool'],
'Data::Dumper' => 'Dumper',
},
};
# Method 3 (This also works with a hashref instead of an arrayref)
use Test2::V1 -include => [
['Test2::Tools::MyTool', 'my_tool', 'my_other_tool'],
['Data::Dumper', 'Dumper'],
];
# Method 4
T2->include('Test2::Tools::MyTool', 'my_tool', 'my_other_tool');
T2->include('Data::Dumper', 'Dumper');
# Using them:
T2->my_tool(...);
T2->Dumper({hi => 'there'});
Note that you MAY override original tools such as ok(), note(), etc. by importing different copies this way. The first time you do this there should be no warnings or errors. If you pull in multiple tools of the same name a redefinition warning is likely. π¬
This also effects exports:
use Test2::V1 -import, -include => ['Data::Dumper'];
print Dumper("Dumper can be imported from your include!");
π«Ά OTHER HANDLE OPTIONS π«Ά
use Test2::V1 T2 => {
include => $ARRAYREF_OR_HASHREF,
namespace => $NAMESPACE,
base => $BASE_PACKAGE // 'Test2::V1::Base',
stomp => $BOOL,
};
include => $ARRAYREF_OR_HASHREFβ see"ADDING TOOLS TO YOUR T2 HANDLE".namespace => $NAMESPACEβ normally a new namespace will be generated for you. You CANNOT rely on the package name being anything specific unless you provide your own.
The namespace here will be where any tools you include will be imported into. It will also have its base class set to the base class you specify, or the Test2::V1::Base module if you do not provide any.
If this namespace already has any symbols defined in it an exception will be thrown unless the stomp argument is set to true (not recommended).
stomp => $BOOLβ used to allow the handle to stomp on an existing namespace (NOT RECOMMENDED). πbase => $BASEβ set the base class from which functions should be inherited. Normally this is set toTest2::V1::Base.
Another interesting use case is to have multiple handles that use eachothers namespaces as base classes:
use Test2::V1;
use Test2::V1::Handle(
'T3',
base => T2->HANDLE_NAMESPACE,
include => {'Alt::Ok' => 'ok'};
);
T3->ok(1, "This uses ok() from Alt::Ok, but all other -> methods are the original");
T3->done_testing(); # Uses the original done_testing
π EXAMPLE USE CASES π
β OVERRIDING INCLUDED TOOLS WITH ALTERNATES β
Lets say you want to use the Test2::Warnings version of warning(), warnings() instead of the Test2::Tools::Warnings versions, and also wanted to import everything else Test2::Warnings provides.
use Test2::V1 -import, -include => ['Test2::Warnings'];
The -include => ['Test2::Warnings'] option means we want to import the default set of imports from Test2::Warnings into our T2() handle's private namespace. This will override any methods that were also previously defined by default. Swapping out the whole fit. π
The -import option means we want to import all subs into the current namespace. This includes anything we got from Test2::Warnings, and we will get the Test2::Warnings version of those subs.
like(
warning { warn 'xxx' }, # This is the Test2::Warnings version of 'warning'
qr/xxx/,
"Got expected warning"
);
π§° TOOLS
okay bestie, this is the motherlode π
every single tool Test2::V1 hands you lives right here, sorted into cute little bundles. Each bundle has a See ... line pointing at the module where that tool actually gets documented, so you can go deeper whenever you want πΈ
π― TARGET
added to Test::V1 in 1.302217 β¨
See Test2::Tools::Target.
You can point at a target class with the -target import argument. And heads up β if you don't give it a target, then $CLASS and CLASS() just won't get imported. No target, no class vars, that's the deal π
use Test2::V1 -target => 'My::Class';
print $CLASS; # My::Class
print CLASS(); # My::Class
Or you can hand it names instead π
use Test2::V1 -target => { pkg => 'Some::Package' };
pkg()->xxx; # Call 'xxx' on Some::Package
$pkg->xxx; # Same
$CLASS
Package variable holding the target class name π
$class = CLASS()
Constant function that hands back the target class name π
β³ DEFER
See Test2::Tools::Defer.
def $func => @args;
added to Test::V1 in 1.302217 π¦
do_def()
also added to Test::V1 in 1.302217 π¦
β BASIC
See Test2::Tools::Basic.
ok($bool, $name) / ok($bool, $name, @diag)
added to Test::V1 in 1.302217 β¨
pass($name) / pass($name, @diag)
added to Test::V1 in 1.302217 β¨
fail($name) / fail($name, @diag)
added to Test::V1 in 1.302217 β¨
diag($message)
added to Test::V1 in 1.302217 π«
note($message)
added to Test::V1 in 1.302217 π«
$todo = todo($reason) / todo $reason => sub { ... }
added to Test::V1 in 1.302217 π
skip($reason, $count)
added to Test::V1 in 1.302217 π
plan($count)
added to Test::V1 in 1.302217 πΈ
skip_all($reason)
added to Test::V1 in 1.302217 πΈ
done_testing()
added to Test::V1 in 1.302217 π₯
bail_out($reason)
added to Test::V1 in 1.302217 π₯
π COMPARE
See Test2::Tools::Compare.
ngl this is the biggest bundle and it's iconic β every single one of these landed in Test::V1 at 1.302217 π
is($got, $want, $name)
added to Test::V1 in 1.302217 π
isnt($got, $do_not_want, $name)
added to Test::V1 in 1.302217 π
like($got, qr/match/, $name)
added to Test::V1 in 1.302217 π
unlike($got, qr/mismatch/, $name)
added to Test::V1 in 1.302217 π
$check = match(qr/pattern/)
added to Test::V1 in 1.302217 π¦
$check = mismatch(qr/pattern/)
added to Test::V1 in 1.302217 π¦
$check = validator(sub { return $bool })
added to Test::V1 in 1.302217 π¦
$check = hash { ... }
added to Test::V1 in 1.302217 π
$check = array { ... }
added to Test::V1 in 1.302217 π
$check = bag { ... }
added to Test::V1 in 1.302217 π
$check = object { ... }
added to Test::V1 in 1.302217 π
$check = meta { ... }
added to Test::V1 in 1.302217 π
$check = number($num)
added to Test::V1 in 1.302217 π
$check = string($str)
added to Test::V1 in 1.302217 π
$check = bool($bool)
added to Test::V1 in 1.302217 π
$check = check_isa($class_name)
added to Test::V1 in 1.302217 π
$check = in_set(@things)
added to Test::V1 in 1.302217 π
$check = not_in_set(@things)
added to Test::V1 in 1.302217 π
$check = check_set(@things)
added to Test::V1 in 1.302217 π
$check = item($thing)
added to Test::V1 in 1.302217 π
$check = item($idx => $thing)
added to Test::V1 in 1.302217 π
$check = field($name => $val)
added to Test::V1 in 1.302217 π
$check = call($method => $expect)
added to Test::V1 in 1.302217 π
$check = call_list($method => $expect)
added to Test::V1 in 1.302217 π
$check = call_hash($method => $expect)
added to Test::V1 in 1.302217 π
$check = prop($name => $expect)
added to Test::V1 in 1.302217 π
$check = check($thing)
added to Test::V1 in 1.302217 β
$check = T()
added to Test::V1 in 1.302217 β
$check = F()
added to Test::V1 in 1.302217 β
$check = D()
added to Test::V1 in 1.302217 β
$check = DF()
added to Test::V1 in 1.302217 β
$check = E()
added to Test::V1 in 1.302217 β
$check = DNE()
added to Test::V1 in 1.302217 β
$check = FDNE()
added to Test::V1 in 1.302217 β
$check = U()
added to Test::V1 in 1.302217 β
$check = L()
added to Test::V1 in 1.302217 β
$check = exact_ref($ref)
added to Test::V1 in 1.302217 π§
end()
added to Test::V1 in 1.302217 π§
etc()
added to Test::V1 in 1.302217 π§
filter_items { grep { ... } @_ }
added to Test::V1 in 1.302217 π§
$check = event $type => ...
added to Test::V1 in 1.302217 π₯
@checks = fail_events $type => ...
added to Test::V1 in 1.302217 π₯
π°οΈ CLASSIC COMPARE
See Test2::Tools::ClassicCompare.
cmp_ok($got, $op, $want, $name)
added to Test::V1 in 1.302217 π¦
πͺ SUBTEST
See Test2::Tools::Subtest.
subtest $name => sub { ... };
added to Test::V1 in 1.302217 π
(Note: This is called subtest_buffered() in the Tools module.)
𧬠CLASS
See Test2::Tools::Class.
can_ok($thing, @methods)
added to Test::V1 in 1.302217 β¨
isa_ok($thing, @classes)
added to Test::V1 in 1.302217 β¨
DOES_ok($thing, @roles)
added to Test::V1 in 1.302217 β¨
π€ ENCODING
See Test2::Tools::Encoding.
set_encoding($encoding)
added to Test::V1 in 1.302217 πΈ
π¦ EXPORTS
See Test2::Tools::Exports.
imported_ok('function', '$scalar', ...)
added to Test::V1 in 1.302217 π
not_imported_ok('function', '$scalar', ...)
added to Test::V1 in 1.302217 π
π REF
See Test2::Tools::Ref.
ref_ok($ref, $type)
added to Test::V1 in 1.302217 π
ref_is($got, $want)
added to Test::V1 in 1.302217 π
ref_is_not($got, $do_not_want)
added to Test::V1 in 1.302217 π
See Test2::Tools::Refcount.
is_refcount($ref, $count, $description)
added to Test::V1 in 1.302217 π
is_oneref($ref, $description)
added to Test::V1 in 1.302217 π
$count = refcount($ref)
added to Test::V1 in 1.302217 π
π MOCK
See Test2::Tools::Mock.
$control = mock ...
added to Test::V1 in 1.302217 π§
$bool = mocked($thing)
added to Test::V1 in 1.302217 π§
π¨ EXCEPTION
See Test2::Tools::Exception.
$exception = dies { ... }
added to Test::V1 in 1.302217 π₯
$bool = lives { ... }
added to Test::V1 in 1.302217 π₯
$bool = try_ok { ... }
added to Test::V1 in 1.302217 π₯
β οΈ WARNINGS
See Test2::Tools::Warnings.
$count = warns { ... }
added to Test::V1 in 1.302217 π
$warning = warning { ... }
added to Test::V1 in 1.302217 π
$warnings_ref = warnings { ... }
added to Test::V1 in 1.302217 π
$bool = no_warnings { ... }
added to Test::V1 in 1.302217 π
π JUSTIFICATION
so likeβ¦ Test2::V0 is a whole rich set of tools, no cap. But bestie, it made a bunch of assumptions about how you'd be using it π and yeah those assumptions are lowkey great if you're a brand-new user writing simple little scripts β but they can absolutely get in your way in sooooo many cases. Let's talk about it π«Ά
π© PROBLEMS WITH V0
assumptions of strict/warnings
lotta people would put their own custom strict/warnings settings right at the top of their testsβ¦ and then they'd use Test2::V0 and the whole thing would get wiped out π gone, deleted, bye.
assumptions of UTF8
sometimes you genuinely do not want that assumption, and that's valid π the way it touches all your regular handles and your test handles, plus how your source gets read, can be a real problem if you're not working with UTF8, or if you have other plans entierly. (their spelling, not mine π)
huge default set of exports, which can grow
sometimes you just want to keep your namespace clean, periodt β¨
and sometimes you import a tool that doesn't conflict with anything in Test2::V0β¦ and then we go and add a new tool that conflicts with yours π we make a point not to break/remove exports, but there's no such commitment about adding new ones. so like, watch out for that.
now the only default export is T2() β which gives you a handle where every tool we expose is available as a method π you can also use the T2 module (not bundled with Test-Simple) for an identical number of keystrokes, which lets you leverage the prototypes on the original tool subroutines. cute, right? π
π SOURCE
the source code repo for Test2-Suite lives at https://github.com/Test-More/test-more/ π go look, bestie.
π οΈ 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/
and that's the whole page bestie, you made it to the end π«Ά this whole thing is from perl v5.44.0, dated 2026-10-17, and it's the Test2::V1(3perl) manpage π slay, go write some tests β¨π
π¦ 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