Test2:...::Spec(3perl)
π NAME π
Test2::Tools::Spec β an RSPEC implementation sitting pretty on top of Test2::Workflow, bestie. π
πΈ DESCRIPTION πΈ
okay so here's the tea: this thing uses Test2::Workflow to serve you an RSPEC variant. and not just any variant β this one? it's got isolation and/or concurrency via forking or threads. she's versatile, she's booked, she's doing both. π§β¨
β¨ SYNOPSIS β¨
use Test2::Bundle::Extended;
use Test2::Tools::Spec;
describe foo => sub {
before_all once => sub { ... };
before_each many => sub { ... };
after_all once => sub { ... };
after_each many => sub { ... };
case condition_a => sub { ... };
case condition_b => sub { ... };
tests foo => sub { ... };
tests bar => sub { ... };
};
done_testing;
like. look at her. the structure. the flow. i'm obsessed actually. π«Ά
π EXPORTS π
all of these functions? same argument pattern, every single time. no cap. π
- the first argument MUST always be a name for the block
- the last argument MUST always be a code reference
- optionally, a configuration hash can be slipped in between the name and the code reference, like a little treat
FUNCTION "name" => sub { ... };
FUNCTION "name" => {...}, sub { ... };
π« NAME π«
the first argument to a Test2::Tools::Spec function MUST be a name. that's it, that's the rule.
does the name need to be unique? no. she doesn't gatekeep. you can reuse names all day, the vibe is what matters. π
π« PARAMS π«
this argument is optional! if you bring it, it should be a hashref. π§Ί
here are the valid keys for that hashref:
flat => $boolβ set this to true and the block will not render as a subtest. instead the events get dumped inline with the parent subtest (or the main test). flat. streamlined. minimal. it's giving "no nesting today." β¨async => $boolβ set to true to mark a block as capable of running concurrently with other test blocks. capable. that does not mean the block WILL be run concurrently β just that it absolutely could be, if the moment's right. πiso => $boolβ set to true if the block MUST be run in isolation. it will run in its own forked process. very main character, very "i need my own space."
β οΈ these tests will be skipped on any platform that does not have true forking, or working/enabled threads.
π§΅ threads will ONLY be used if theT2_WORKFLOW_USE_THREADSenv var is set. thread tests are only run if theT2_DO_THREAD_TESTSenv var is set. both flags, or no threads bestie, that's the deal.
todo => $reasonβ use this to mark an entire block as TODO. she's on the list, she's just not on the schedule yet. πskip => $reasonβ use this to prevent a block from running at all. ghosted. blocked. not today. π
π« CODEREF π«
this argument is required, no negotiations. it should be a code reference that will run some assertions. periodt. π
π¦ ESSENTIALS π¦
tests NAME => sub { ... }
tests NAME => \%params, sub { ... }
tests($NAME, \%PARAMS, \&CODE)
tests NAME => sub { ... }
tests NAME => \%params, sub { ... }
tests($NAME, \%PARAMS, \&CODE)
this defines a test block. test blocks are essentially subtests. all test blocks will be run, and they're expected to produce events. π test blocks can run multiple times if the case() function is also in the mix.
it() is an alias to tests(). same girl, different name tag. π
π« these ARE NOT inherited by nested describe blocks.
case NAME => sub { ... }
case NAME => \%params, sub { ... }
case($NAME, \%PARAMS, \&CODE)
case NAME => sub { ... }
case NAME => \%params, sub { ... }
case($NAME, \%PARAMS, \&CODE)
this lets you specify multiple conditions in which the test blocks should be run. every test block within the same group (that's a "describe") will be run once per case. so if you've got three cases, your tests show up three times. booked and busy. π₯
π« these ARE NOT inherited by nesteddescribeblocks, but nesteddescribeblocks will be executed once per case.
before_each NAME => sub { ... }
before_each NAME => \%params, sub { ... }
before_each($NAME, \%PARAMS, \&CODE)
before_each NAME => sub { ... }
before_each NAME => \%params, sub { ... }
before_each($NAME, \%PARAMS, \&CODE)
specify a codeblock that should be run multiple times β once before each tests() block runs. these will run AFTER case() blocks but before tests() blocks. right in that sweet spot. π
β
these ARE inherited by nested describe blocks.
before_case NAME => sub { ... }
before_case NAME => \%params, sub { ... }
before_case($NAME, \%PARAMS, \&CODE)
before_case NAME => sub { ... }
before_case NAME => \%params, sub { ... }
before_case($NAME, \%PARAMS, \&CODE)
same as before_each(), except these blocks run BEFORE case() blocks. early bird energy. βοΈ
π« these ARE NOT inherited by nested describe blocks.
before_all NAME => sub { ... }
before_all NAME => \%params, sub { ... }
before_all($NAME, \%PARAMS, \&CODE)
before_all NAME => sub { ... }
before_all NAME => \%params, sub { ... }
before_all($NAME, \%PARAMS, \&CODE)
specify a codeblock that should be run once, before all the test blocks run. one and done. iconic. β¨
π« these ARE NOT inherited by nested describe blocks.
around_each NAME => sub { ... }
around_each NAME => \%params, sub { ... }
around_each($NAME, \%PARAMS, \&CODE)
around_each NAME => sub { ... }
around_each NAME => \%params, sub { ... }
around_each($NAME, \%PARAMS, \&CODE)
specify a codeblock that should wrap around each test block. these blocks are run AFTER case blocks, but before test blocks.
around_each wrapit => sub {
my $cont = shift;
local %ENV = ( ... );
$cont->();
...
};
the first argument to the codeblock will be a callback that MUST be called somewhere inside the sub in order for nested items to run. like, actually. if you don't call it, nothing downstream happens. it's the whole vibe gate. πͺπ«
β
these ARE inherited by nested describe blocks.
around_case NAME => sub { ... }
around_case NAME => \%params, sub { ... }
around_case($NAME, \%PARAMS, \&CODE)
around_case NAME => sub { ... }
around_case NAME => \%params, sub { ... }
around_case($NAME, \%PARAMS, \&CODE)
same as "around_each" except these run BEFORE case blocks. same energy, earlier slot. π
π« these ARE NOT inherited by nested describe blocks.
around_all NAME => sub { ... }
around_all NAME => \%params, sub { ... }
around_all($NAME, \%PARAMS, \&CODE)
around_all NAME => sub { ... }
around_all NAME => \%params, sub { ... }
around_all($NAME, \%PARAMS, \&CODE)
same as "around_each" except that it only runs once to wrap ALL test blocks. one wrapper to rule them all, no cap. π
π« these ARE NOT inherited by nested describe blocks.
after_each NAME => sub { ... }
after_each NAME => \%params, sub { ... }
after_each($NAME, \%PARAMS, \&CODE)
after_each NAME => sub { ... }
after_each NAME => \%params, sub { ... }
after_each($NAME, \%PARAMS, \&CODE)
same as "before_each" except it runs right after each test block. cleanup crew represent. π§Ή
β
these ARE inherited by nested describe blocks.
after_case NAME => sub { ... }
after_case NAME => \%params, sub { ... }
after_case($NAME, \%PARAMS, \&CODE)
after_case NAME => sub { ... }
after_case NAME => \%params, sub { ... }
after_case($NAME, \%PARAMS, \&CODE)
same as "after_each" except it runs right after the case block, and before the test block. sandwiched. π₯ͺ
π« these ARE NOT inherited by nested describe blocks.
after_all NAME => sub { ... }
after_all NAME => \%params, sub { ... }
after_all($NAME, \%PARAMS, \&CODE)
after_all NAME => sub { ... }
after_all NAME => \%params, sub { ... }
after_all($NAME, \%PARAMS, \&CODE)
same as "before_all" except it runs after all test blocks have been run. the closing credits. π¬
π« these ARE NOT inherited by nested describe blocks.
π§ SHORTCUTS π§
these are shortcuts, bestie. each one is the same as tests() except some parameters get added for you. free garnish. β¨
β οΈ these are NOT exported by default/. (yes, that trailing slash is in the original and yes, we're keeping it. character. π )
mini NAME => sub { ... }
same as:
tests NAME => { flat => 1 }, sub { ... }
iso NAME => sub { ... }
same as:
tests NAME => { iso => 1 }, sub { ... }
miso NAME => sub { ... }
same as:
tests NAME => { mini => 1, iso => 1 }, sub { ... }
async NAME => sub { ... }
same as:
tests NAME => { async => 1 }, sub { ... }
π¨ note: this conflicts with theasync()exported fromthreads. don't import both. pick a lane, babe.
masync NAME => sub { ... }
same as:
tests NAME => { minit => 1, async => 1 }, sub { ... }
π CUSTOM ATTRIBUTE DEFAULTS π
sometimes you want default attributes applied to all your tests() or case() blocks. and you CAN do that. and it's lexical to your describe or package root. so it knows its scope. she's self-aware. π
use Test2::Bundle::Extended;
use Test2::Tools::Spec ':ALL';
# All 'tests' blocks after this declaration will have C<<iso => 1>> by default
spec_defaults tests => (iso => 1);
tests foo => sub { ... }; # isolated
tests foo, {iso => 0}, sub { ... }; # Not isolated
spec_defaults tests => (iso => 0); # Turn it off again
defaults are inherited by nested describe blocks. you can ALSO override the defaults for the scope of the describe:
spec_defaults tests => (iso => 1);
describe foo => sub {
spec_defaults tests => (async => 1); # Scoped to this describe and any child describes
tests bar => sub { ... }; # both iso and async
};
tests baz => sub { ... }; # Just iso, no async.
and you can apply defaults to any type of block:
spec_defaults case => (iso => 1); # All cases are 'iso';
β οΈ defaults are not inherited when a builder's return is captured.
spec_defaults tests => (iso => 1);
# Note we are not calling this in void context, that is the key here.
my $d = describe foo => {
tests bar => sub { ... }; # Not iso
};
see that? captured return = the default doesn't come with. that's the whole key. iykyk. π
π₯ EXECUTION ORDER π₯
as each function is encountered, it executes. just like any other function. no waiting room. π’
describe() will immediately execute the codeblock it's given. all the other functions will stash their codeblocks to be run later. then, when done_testing() runs, the workflow gets compiled β and at that point all the other blocks run. the queue empties. the show begins. π¬β¨
here's an overview of the order in which blocks get called once compiled (at done_testing()):
before_all
for-each-case {
before_case
case
after_case
# AND/OR nested describes
before_each
tests
after_each
}
after_all
like. the symmetry. the structure. i'm not crying, you're crying. ππ
π¦ SOURCE π¦
the source code repository for Test2-Workflow can be found at https://github.com/Test-More/test-more/.
go. star it. she deserves it. β
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
π¦ AUTHORS π¦
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist7@gmail.com>.
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!! ππ«Ά this manual page is brought to you by perl v5.44.0, dated 2026-10-17, and it ships as Test2:...::Spec(3perl) β mwah, go write some specs, you've got this π β¨πΈ
π¦ 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