Test2::Harness::Runner::Preload - DSL for building complex stage-based preload tools.


Test2::Harness allows you to preload libraries for a performance boost. This module provides tools that let you go beyond that and build a more complex preload. In addition you can build multiple preload stages, each stage will be its own process and tests can run from a specific stage. This allows for multiple different preload states from which to run tests.



The -P or --preload options work for custom preload modules just as they do regular modules. Yath will know the difference and act accordingly.

    yath test -PMy::Preload


    package My::Preload;
    use strict;
    use warnings;

    # This imports several useful tools, and puts the necessary meta-data in
    # your package to identify it as a special preload.
    use Test2::Harness::Runner::Preload;

    # You must specify at least one stage.
    stage Moose => sub {
        # Preload can be called multiple times, and can load multiple modules
        # per call. Order is preserved.
        preload 'Moose', 'Moose::Role';
        preload 'Scalar::Util', 'List::Util';

        # preload can also be given a sub if you have some custom code to run
        # at a specific point in the load order
        preload sub {
            # Do something before loading Try::Tiny

        preload 'Try::Tiny';

        # Eager means tests from nested stages can be run in this stage as
        # well, this is useful if the nested stage takes a long time to load as
        # it allows yath to start running tests sooner instead of waiting for
        # the stage to finish loading. Once the nested stage is loaded tests
        # intended for it will start running from it instead.

        # default means this stage is the one to use if the test does not
        # specify a stage.

        # These are hooks that let you run arbitrary code at specific points in
        # the process. pre_fork happens just before forking to run a test.
        # post_fork happens just after forking for a test. pre_launch happens
        # as late as possible before the test starts executing (post fork,
        # after $0 and other special state are reset).
        pre_fork sub { ... };
        post_fork sub { ... };
        pre_launch sub { ... };

        # Stages can be nested, nested ones build off the previous stage, but
        # are in a forked process to avoid contaminating the parent.
        stage Types => sub {
            preload 'MooseX::Types';

    # Alternative stage that loads Moo instead of Moose
    stage Moo => sub {
        preload 'Moo';



$meta = $class->TEST2_HARNESS_PRELOAD()

This export provides the meta object, which is an instance of this class. This method being present is how Test2::Harness differentiates between a regular module and a special preload library.

stage NAME => sub { ... }

This creates a new stage with the given NAME, and then runs the coderef with the new stage set as the active one upon which the other function here will operate. Once the coderef returns the active stage is cleared.

You may nest stages by calling this function again inside the codeblock.

NOTE: stage names ARE case sensitive. This can be confusing when you consider that most harness directives are all-caps. In the following case the stage requested by the test and the stage defined in the library are NOT the same.

In a test file:


In a preload library:

    stage foo { ... }

Harness directives are all-caps, however the user data portion need not be, this is fine:


However it is very easy to make the mistake of thinking it is case insensitive. It is also easy to assume the 'foo' part of the harness directive must be all caps. In many cases it is smart to make your stage names all-caps.

preload $module_name
preload @module_names
preload sub { ... }

This MUST be called inside a stage() builder coderef.

This adds modules to the list of libraries to preload. Order is preserved. You can also add coderefs to execute arbitrary code between module loads.

The coderef is called with no arguments, and its return is ignored.


This MUST be called inside a stage() builder coderef.

This marks the active stage as being eager. An eager stage will start running tests for nested stages if it finds itself with no tests of its own to run before the nested stage can finish loading. The idea here is to avoid unused test slots when possible allowing for tests to complete sooner.


This MUST be called inside a stage() builder coderef.

This MUST be called only once across ALL stages in a given library.

If multiple preload libraries are loaded then the first default set (based on load order) will be the default, others will notbe honored.

$stage_name = file_stage($test_file)

This is optional. If defined this callback will have a chance to look at all files that are going to be run and assign them a stage. This may return undef or an empty list if it does not have a stage to assign.

If multiple preload libraries define file_stage callbacks they will be called in order, the first one to return a stage name will win.

If no file_stage callbacks provide a stage for a file then any harness directives declaring a stage will be honored. If no stage is ever assigned then the test will be run int he default stage.

pre_fork sub { ... }

This MUST be called inside a stage() builder coderef.

Add a callback to be run just before the preload-stage process forks to run the test. Note that any state changes here can effect future tests to be run.

post_fork sub { ... }

This MUST be called inside a stage() builder coderef.

Add a callback to be run just after the preload-stage process forks to run the test. This is run as early as possible, things like $0 may not be set properly yet.

pre_launch sub { ... }

This MUST be called inside a stage() builder coderef.

Add a callback to be run just before control of the test process is turned over to the test file itself. This is run as late as possible, so things like $0 should be set properly.


This class is also the meta-object used to construct a preload library. The methods are left undocumented as this is an implementation detail and you are not intended to directly use this object.


The source code repository for Test2-Harness can be found at


Chad Granum <>


Chad Granum <>


Copyright 2020 Chad Granum <>.

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