Test2::Tools::Spec(3) Perl Programmers Reference Guide 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 { ... };
The first argument to a Test2::Tools::Spec function MUST be a name. The name does not need to be unique.
This argument is optional. If present this should be a hashref.

Here are the valid keys for the hashref:

If this is set to true then the block will not render as a subtest, instead the events will be inline with the parent subtest (or main test).
Set this to true to mark a block as being capable of running concurrently with other test blocks. This does not mean the block WILL be run concurrently, just that it can be.
Set this to true if the block MUST be run in isolation. If this is true then the block will run in its own forked process.

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.

Use this to mark an entire block as TODO.
Use this to prevent a block from running at all.
This argument is required. This should be a code reference that will run some assertions.

This defines a test block. Test blocks are essentially subtests. All test blocks will be run, and are expected to produce events. Test blocks can run multiple times if the case() function is also used.

it() is an alias to tests().

These ARE NOT inherited by nested describe blocks.

This lets you specify multiple conditions in which the test blocks should be run. Every test block within the same group ("describe") will be run once per case.

These ARE NOT inherited by nested describe blocks, but nested describe blocks will be executed once per case.

Specify a codeblock that should be run multiple times, once before each tests() block is run. These will run AFTER case() blocks but before tests() blocks.

These ARE inherited by nested describe blocks.

Same as before_each(), except these blocks run BEFORE case() blocks.

These ARE NOT inherited by nested describe blocks.

Specify a codeblock that should be run once, before all the test blocks run.

These ARE NOT inherited by nested describe blocks.

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.

These ARE inherited by nested describe blocks.

Same as "around_each" except these run BEFORE case blocks.

These ARE NOT inherited by nested describe blocks.

Same as "around_each" except that it only runs once to wrap ALL test blocks.

These ARE NOT inherited by nested describe blocks.

Same as "before_each" except it runs right after each test block.

These ARE inherited by nested describe blocks.

Same as "after_each" except it runs right after the case block, and before the test block.

These ARE NOT inherited by nested describe blocks.

Same as "before_all" except it runs after all test blocks have been run.

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/.

Same as:

    tests NAME => { flat => 1 }, sub { ... }
    
Same as:

    tests NAME => { iso => 1 }, sub { ... }
    
Same as:

    tests NAME => { mini => 1, iso => 1 }, sub { ... }
    
Same as:

    tests NAME => { async => 1 }, sub { ... }
    

Note: This conflicts with the async() exported from threads. Don't import both.

Same as:

    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/

2024-04-28 perl v5.40.0