NAME

Test2::Aggregate - Aggregate tests for increased speed

SYNOPSIS

use Test2::Aggregate;
use Test2::V0; # Or 'use Test::More' etc if your suite uses an other framework

Test2::Aggregate::run_tests(
    dirs => [@test_dirs_or_files]
);

done_testing();

VERSION

See $VERSION in module.

DESCRIPTION

Aggregates all tests specified with dirs (which can even be individual tests) to avoid forking, reloading etc. that can help with performance (dramatically if you have numerous small tests) and also facilitate group profiling. It is quite common to have tests that take over a second of startup time for milliseconds of actual runtime - Test2::Aggregate removes that overhead. Test files are expected to end in .t and are run as subtests of a single aggregate test.

A bit similar (mainly in intent) to Test::Aggregate, but no inspiration was drawn from the specific module, so simpler in concept and execution, which makes it much more likely to work with your test suite (especially if you use modern tools like Test2::Suite). It does not even try to package each test by default (there is an option), which may be good or bad, depending on your requirements.

Generally, the way to use this module is to try to aggregate sets of quick tests (e.g. unit tests). Try to iteratively add tests to the aggregator, using the lists option, so you can easily edit and remove those that do not work. Trying an entire, large, suite in one go is not a good idea, as an incompatible test can break the run making the subsequent tests fail (especially when doing things like globally redefining built-ins etc.) - see the module usage notes for help.

The module can work with Test::Builder / Test::More suites, but you will have less issues with Test2::Suite (see notes).

METHODS

run_tests

my $stats = Test2::Aggregate::run_tests(
    dirs          => \@dirs,              # optional if lists defined
    lists         => \@lists,             # optional if dirs defined
    exclude       => qr/exclude_regex/,   # optional
    include       => qr/include_regex/,   # optional
    root          => '/testroot/',        # optional
    load_modules  => \@modules,           # optional
    package       => 0,                   # optional
    shuffle       => 0,                   # optional
    sort          => 0,                   # optional
    reverse       => 0,                   # optional
    unique        => 1,                   # optional
    repeat        => 1,                   # optional, requires Test2::Plugin::BailOnFail for < 0
    slow          => 0,                   # optional
    test_warnings => 0,                   # optional
    override      => \%override,          # optional, requires Sub::Override
    stats_output  => $stats_output_path,  # optional
    extend_stats  => 0,                   # optional
    pass_only     => 0,                   # optional
    absolute      => 0,                   # optional
    bail_on_fail  => 0,                   # optional, requires Test2::Plugin::BailOnFail
    no_dot        => 0,                   # optional
    relative_root => 0,                   # optional
    allow_errors  => 0,                   # optional
    pre_eval      => $code_to_eval,       # optional
    dry_run       => 0,                   # optional
    recursion     => 0,                   # optional
    slurp_param   => {binmode => $mode}   # optional
);

Runs the aggregate tests. Returns a hashref with stats like this:

$stats = {
  'test.t' => {
    'test_no'   => 1,                 # numbering starts at 1
    'pass_perc' => 100,               # for single runs pass/fail is 100/0
    'timestamp' => '20190705T145043', # start of test
    'time'      => '0.1732',          # seconds - only with stats_output
    'warnings'  => $STDERR            # only with test_warnings on non empty STDERR
  }
};

The parameters to pass:

HELPER SCRIPTS

agg (Test2::Aggregate harness wrapper)

agg [options] <file/dir1 ...>

Pass a list of Perl test files/directories and they will run aggregated via yath (or prove if specified). It is not meant to be used for .t files that use Test2::Aggregate themselves.

It is useful either to speed up a test run for tests you know can be aggregated, or to check whether specific tests can run successfully under Test2::Aggregate.

Options:
--out <s>,          -o <s>  : Specify the test file to be created (tmp file by default).
--lists,            -l      : Use files specified as lists.
--prove,            -p      : Force prove (default is yath/Test2 if installed).
--verbose,          -v      : Verbose (passed to yath/prove)
--absolute,         -a      : Use absolute paths in generated files. Disabled with -r.
--include_regex <s> -ir <s> : Regex for test paths to include.
--exclude_regex <s> -er <s> : Regex for test paths to exclude.
--root <s>          -r <s>  : Define a custom root dir (default is current dir).
--include <s>,      -I <s>  : Library paths to include.
--test_warnings,    -w      : Fail tests on warnings.
--test_bundle <s>,  -t      : Test bundle (def: Test2::V0 or Test::More if -p). Can be comma-separated list.
--pass_output <s>,  -po <s> : Output directory for list of 100% passing tests.
--reverse                   : Reverse the test order.
--shuffle                   : Shuffle the test order.
--sort                      : Run the tests alphabetically.
--stats_output <s>, -so <s> : Stats output directory (does not combine with pass_output).
--help              -h      : Show basic help and exit.

aggit (Test2::Aggregate iterative aggregator)

aggit [options] <file/dir1 ...>

Runs tests through an aggregate test repeatedly. On each failed run, the active test file is removed and the remaining set is tried again. Requires Test2::Plugin::BailOnFail. Startup failures and failures after all test files have completed abort the command with a nonzero exit status instead of removing tests. This is a coarse way to discover a starting list of tests that can be aggregated together, not proof that each removed test is independently broken.

--sort, --shuffle, and --reverse are applied once during discovery. The resulting order is preserved across retries and in the generated runner. By default, list entries are relative to the generated runner's directory; --absolute writes absolute paths. --root resolves inputs against a custom directory. --exclude_regex and --exclude_list filter the discovered tests.

aggit passes --event_timeout SECONDS (alias -et, default 10) to yath as --et SECONDS. This is an inactivity timeout, not a total runtime limit; Harness normally defaults to 60 seconds. The timeout applies to the generated aggregate runner, so # HARNESS-NO-TIMEOUT comments in individual aggregated files do not disable it. Under --prove, or automatic fallback to prove when Harness is unavailable, this timeout is not provided and hung tests require external supervision.

For agg, --pass_output takes precedence over --stats_output when both are supplied.

Common helper workflows

Try known-safe tests through the default Test2::Harness runner:

agg t/unit t/lib

Force prove and Test::More compatibility mode:

agg --prove t/unit

Generate a directory containing a list of tests that passed when aggregated:

agg --root . --pass_output pass t

--root . makes the passing paths relative to the current project directory, rather than the temporary aggregate runner's directory.

Iteratively discover an aggregateable list and generated aggregate test:

aggit --out aggregate-candidate t

Run the generated aggregate test later (its list paths are runner-relative):

yath test aggregate-candidate.t

USAGE NOTES

Not all tests can be modified to run under the aggregator, it is not intended for tests that require an isolated environment, do overrides etc. For other tests which can potentially run under the aggregator, sometimes very simple changes may be needed like giving unique names to subs (or not warning for redefines, or trying the package option), replacing things that complain, restoring the environment at the end of the test etc.

Unit tests are usually great for aggregating. You could use the hash that run_tests returns in a script that tries to add more tests automatically to an aggregate list to see which added tests passed and keep them, dropping failures. See later in the notes for a detailed example.

Trying to aggregate too many tests into a single one can be counter-intuitive as you would ideally want to parallelize your test suite (so a super-long aggregated test continuing after the rest are done will slow down the suite). And in general more tests will run aggregated if they are grouped so that tests that can't be aggregated together are in different groups.

You can call Test2::Aggregate::run_tests multiple times in a test. Nested calls are skipped unless the inner call enables recursion. Loading Test2::Plugin::BailOnFail through bail_on_fail or repeat < 0 affects subsequent tests too: a later failure will also trigger a bail-out.

Version 1.00 path compatibility

Stats keys and warning labels are root-relative by default in 1.00. Set absolute if you need the previous full rooted paths in stats output.

Relative test paths that do not start with /, ., or ./ are now executed with a ./ prefix so they keep working on perls where . is not in @INC. Set no_dot if you need the previous behaviour.

Nested aggregate runs are skipped by default when $ENV{AGGREGATE_TESTS} is already set. Set recursion only for suites where nested aggregate execution is intentional.

Test::More

If you haven't switched to the Test2::Suite you are generally advised to do so for a number of reasons, compatibility with this module being only a very minor one. If you are stuck with a Test::More suite, Test2::Aggregate can still probably help you more than the similarly-named Test::Aggregate... modules.

Although the module tries to load Test2 with minimal imports to not interfere, it is generally better to do use Test::More; in your aggregating test (i.e. alongside with use Test2::Aggregate).

Test::Builder

To avoid warnings, if you are importing Test::Builder in multiple aggregated tests, you will want to import first on the aggregating test. It might look like this:

use Test::Builder;
use Test2::V0;
use Test2::Aggregate;

Test2::Aggregate::run_tests( ... );

BEGIN / END Blocks

BEGIN blocks run as each test file is compiled. END blocks are deferred until the aggregate process exits, not the end of each individual test file. Cleanup needed between files must therefore happen before the file returns. Global overrides can affect later tests. One example is a *CORE::GLOBAL::exit override to test scripts that can call exit(). A solution is to use something like Test::Trap:

BEGIN {
    unless ($Test::Trap::VERSION) { # Avoid warnings for multiple loads in aggregation
        require Test::Trap;
        Test::Trap->import();
    }
}

Test::Class

Test::Class is sort of an aggregator itself. You make your tests into modules and then load them on the same .t file, so ideally you will not end up with many .t files that would require further aggregation. If you do, due to the Test::Class implementation specifics, those .t files won't run under Test2::Aggregate.

$0 variable

Remember the $0 variable is the aggregate runner, not the test file.

Mocks, CORE::GLOBAL::* Overrides

Always prefer mocks (Test2::Mock) instead of global built-ins where possible for aggregated tests. Remember to clear mocks at the end of test.

$ENV{AGGREGATE_TESTS}

The environment variable AGGREGATE_TESTS will be set while the tests are running for your convenience. Example usage is making a test you know cannot run under the aggregator check and croak if it was run under it, or a module that can only be loaded once, so you load it on the aggregated test file and then use something like this in the individual test files:

eval 'use My::Module' unless $ENV{AGGREGATE_TESTS};

If you have a custom test bundle, you could use the variable to do things like disable warnings on redefines only for tests that run aggregated:

use Import::Into;

sub import {
   ...
   'warnings'->unimport::out_of($package, 'redefine')
       if $ENV{AGGREGATE_TESTS};
}

Another idea is to make the test die when it is run under the aggregator, if, at design time, you know it is not supposed to run aggregated.

Example aggregating strategy

There are many approaches you could do to use Test2::Aggregate with an existing test suite, usually involving an iterative process of trying to run several tests aggregated, seeing if you can fix the failing ones, otherwise you remove them from the aggregation etc.

This process can be done with the help of the agg script. For example, to try all tests under t/ aggregated and a list of passing tests put under the pass directory you would do:

> agg --root . --pass_output pass t

(which will use yath & Test2::V0, you can do agg --prove --root . --pass_output pass t for prove & Test::More)

If the run completes, thanks to --pass_output you have a "starting point" i.e. a .txt list that can run under the aggregator with the lists option:

Test2::Aggregate::run_tests(
    lists => ['pass/name_of_file.txt']
);

If the run does not complete (e.g. signal 11 on some test), try fewer tests by choosing just a subdirectory. If that's not possible, you'll probably have to go to the more manual method of getting a full list of your tests (find t -name '*.t' > all.lst) then trying to run parts of it, again with the lists option.

After you have a starting point, you can try see if there is an obvious reason some tests fail and address it to add them back to the pass list. You can even try adding back some of the failed tests that were not among the first to fail - test failures can sometimes be cascading, so some might be passing if added back, or have small issues you can address.

Try adding test_warnings => 1 to run_tests to fix warnings as well, unless it is common for your tests to have STDERR output.

In the end, you will end up with part of your tests aggregated in (multiple if you want to run them in parallel) list files, with the rest of your tests to be run non-aggregated.

You don't actually have to move and separate aggregated/non-aggregated files when using lists, you can still have your entire suite run the aggregated tests once and not repeat them along with the other, non-aggregated tests, by taking advantage of the --exclude-list option of the Test2::Harness.

Hopefully your tests can run in parallel (prove/yath -j), in which case you would split your aggregated tests into multiple lists to have them run in parallel. Here is an example of a wrapper around yath, to easily handle multiple lists:

BEGIN {
    my @args = ();
    foreach (@ARGV) {
        if (/--exclude-lists=(\S+)/) {
            my $all = 't/aggregate/aggregated.tests';
            `awk '{print "t/"\$0}' $1 > $all`;
            push @args, "--exclude-list=$all";
        } else { push @args, $_ if $_; }
    }
    push @args, qw(-P...) # Preload module list (useful for non-aggregated tests)
        unless grep {/--cover/} @args;
    @ARGV = @args;
}
exec ('yath', @ARGV);

You would call it with something like --exclude-lists=t/aggregate/*.lst, and the tests listed will be excluded (you will have them running aggregated through their own .t files using Test2::Aggregate).

WINDOWS SUPPORT

Only filesystems with the / separator are supported, simply because the author does not expect Windows users (many test suites don't work under StrawberryPerl etc). However, if you want Windows filesystems to be supported, it should be simple enough as long as you are willing to help with testing - just ask.

DEVELOPMENT AND RELEASES

README.md is generated from this module's POD using Pod::Markdown. Install author dependencies, then run:

perl Makefile.PL
make readme
AUTHOR_TESTING=1 prove -Ilib xt/author xt/integration
make disttest

make readme refreshes the README. make distdir (also used by make dist and make disttest) refreshes it automatically before copying distribution files. These author targets require Pod::Markdown, but normal installation and make test do not. The README author test detects stale generated content; review and commit README changes alongside the POD. Set the release date in Changes when preparing the final release.

AUTHOR

Dimitrios Kechagias, https://metacpan.org/author/DKECHAG

BUGS

Please report any bugs or feature requests to bug-test2-aggregate at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test2-Aggregate. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. You could also submit issues or even pull requests to the github repo (see below).

GIT

https://github.com/SpareRoom/Test2-Aggregate

COPYRIGHT & LICENSE

Copyright (C) 2019, SpareRoom.com

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.