Test2::Tools::Spec(3pm)

NAME
Test2::Tools::Spec - RSPEC implementation on top of Test2::Workflow

DESCRIPTION
This uses Test2::Workflow to implement an RSPEC variant. This variant supports isolation and/or concurrency via forking or threads.

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;

EXPORTS
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 { ... };

NAME
    The first argument to a Test2::Tools::Spec function MUST be a name. The name does not need to be unique.

PARAMS
    This argument is optional. If present this should be a hashref.

    Here are the valid keys for the hashref:

    flat => $bool
        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).

    async => $bool
        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.

    iso => $bool
        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.

    todo => $reason
        Use this to mark an entire block as TODO.

    skip => $reason
        Use this to prevent a block from running at all.

CODEREF
    This argument is required. This should be a code reference that will run some assertions.

  ESSENTIALS

tests NAME => sub { ... }
tests NAME => \%params, sub { ... }
tests($NAME, \%PARAMS, \&CODE)
it NAME => sub { ... }
it NAME => \%params, sub { ... }
it($NAME, \%PARAMS, \&CODE)
    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.

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 ("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.

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 is run. These will run AFTER case() blocks but before tests() blocks.

    These ARE inherited by nested describe blocks.

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.

    These ARE NOT inherited by nested describe blocks.

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.

    These ARE NOT inherited by nested describe blocks.

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.

    These ARE inherited by nested describe blocks.

around_case NAME => sub { ... }
around_case NAME => \%params, sub { ... }
around_case($NAME, \%PARAMS, \&CODE)
    Same as "around_each" except these run BEFORE case blocks.

    These ARE NOT inherited by nested describe blocks.

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.

    These ARE NOT inherited by nested describe blocks.

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.

    These ARE inherited by nested describe blocks.

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.

    These ARE NOT inherited by nested describe blocks.

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.

    These ARE NOT inherited by nested describe blocks.

  SHORTCUTS
These are shortcuts. Each of these is the same as tests() except some parameters are added for you.

These are NOT exported by default/.

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 the async() exported from threads. Don't import both.

masync NAME => sub { ... }
    Same as:

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

  CUSTOM ATTRIBUTE DEFAULTS
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
    };

EXECUTION ORDER
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

SOURCE
The source code repository for Test2-Workflow can be found at https://github.com/Test-More/Test2-Suite/.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS
Chad Granum <exodist@cpan.org>

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