tokuhirom's Blog

Filter subtests by environment variables

Sometime I skip some subtests for fast testing.

This is just a *hack* to skip only blocks matched the block name by environment variable.

You would add following lines to your t/Util.pm

{
    no warnings 'redefine';
    my $ORIGIN_subtest = \&Test::More::subtest;
    our $SUBTEST_FLAG;
    *Test::More::subtest = sub {
        if (my $filter = $ENV{SUBTEST_FILTER}) {
            if ($_[0] =~ qr{\Q$filter\E} || $SUBTEST_FLAG) {
                local $SUBTEST_FLAG = 1;
                local $Test::Builder::Level = $Test::Builder::Level + 1;
                $ORIGIN_subtest->(@_);
            } else {
                print  {Test::More->builder->failure_output} "# skip $_[0] by SUBTEST_FILTER\n";
            }
        } else {
            goto &$ORIGIN_subtest;
        }
    };
}

【20111017 20:41 更新】
ネストした場合のことをかんがえていなかったのを修正。