Test2::Tools::Spec(3) | User Contributed Perl Documentation | Test2::Tools::Spec(3) |
Test2::Tools::Spec - RSPEC implementation on top of Test2::Workflow
This uses Test2::Workflow to implement an RSPEC variant. This variant supports isolation and/or concurrency via forking or threads.
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;
All of these use the same argument pattern. 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 inserted between the name and the code reference.
FUNCTION "name" => sub { ... }; FUNCTION "name" => {...}, sub { ... };
Here are the valid keys for the hashref:
These tests will be skipped on any platform that does not have true forking, or working/enabled threads.
Threads will ONLY be used if the T2_WORKFLOW_USE_THREADS env var is set. Thread tests are only run if the T2_DO_THREAD_TESTS env var is set.
"it()" is an alias to "tests()".
These ARE NOT inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks, but nested describe blocks will be executed once per case.
These ARE inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks.
These ARE NOT inherited by nested describe 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.
These ARE inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks.
These ARE inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks.
These ARE NOT inherited by nested describe blocks.
These are shortcuts. Each of these is the same as "tests()" except some parameters are added for you.
These are NOT exported by default/.
tests NAME => { flat => 1 }, sub { ... }
tests NAME => { iso => 1 }, sub { ... }
tests NAME => { mini => 1, iso => 1 }, sub { ... }
tests NAME => { async => 1 }, sub { ... }
Note: This conflicts with the "async()" exported from threads. Don't import both.
tests NAME => { minit => 1, async => 1 }, sub { ... }
Sometimes you want to apply default attributes to all "tests()" or "case()" blocks. This can be done, and is lexical to your describe or package root!
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.
You can apply defaults to any type of blocks:
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 };
As each function is encountered it executes, just like any other function. The "describe()" function will immediately execute the codeblock it is given. All other functions will stash their codeblocks to be run later. When "done_testing()" is run the workflow will be compiled, at which point all other blocks will run.
Here is 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
The source code repository for Test2-Workflow can be found at https://github.com/Test-More/Test2-Suite/.
Copyright 2018 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/
2020-10-22 | perl v5.34.0 |