TEST2::MANUAL::TOOLING::SUBTEST(3perl)

πŸŽ€ Test2::Manual::Tooling::Subtest πŸ’…

okay bestie, welcome to Test2::Manual::Tooling::Subtest β€” this one's all about how to build a tool that makes use of subtests. 🫢


✨ DESCRIPTION ✨

Subtests are that girl. They're a nice way of making related events look visually distinct, and architecturally distinct too. Two kinds of separation for the price of one, we love. πŸ’–


🌸 WHICH TYPE OF SUBTEST DO I NEED? 🌸

so there are 2 types of subtest, and you gotta know which one you're dealing with:

  1. subtests with user-supplied coderefs β€” like the subtest() function itself, where the user hands you their own block of code
  2. subtests that do not have any user supplied coderefs

so which one do you need? bestie it's actually so simple: if you're gonna let the user define the subtest with their own codeblock, you've got the first type. otherwise? second type. that's it. πŸ’…

but either way β€” either way β€” you still need the same API function:

Test2::API::run_subtest

periodt. πŸŽ€


πŸ¦‹ SUBTEST WITH USER SUPPLIED CODEREF πŸ¦‹

this example is gonna emulate the subtest function. watch:

use Test2::API qw/context run_subtest/;

sub my_subtest {
    my ($name, $code) = @_;

    # Like any other tool, you need to acquire a context, if you do not then
    # things will not report the correct file and line number.
    my $ctx = context();

    my $bool = run_subtest($name, $code);

    $ctx->release;

    return $bool;
}

ngl this looks incredibly simple... and it is, because run_subtest() does all the hard work for you. 😭 it'll issue a Test2::Event::Subtest event carrying the results of the subtest. and the subtest event itself will report to the proper file and line number thanks to the context you acquired β€” even though it doesn't look like you used the context. sneaky iconic behavior. πŸ‘‘

and plot twist: run_subtest() can take extra arguments too:

run_subtest($name, $code, \%params, @args);

⭐ @args

this lets you pass arguments into the codeblock that gets run. yes bestie, the codeblock gets fed. πŸ“

⭐ \%params

this is a hashref of parameters. currently there are 3 possible parameters:

  • buffered => $bool β€” this turns the subtest into the new style buffered subtest. this type of subtest is recommended, but it is not default. so like, do the recommended thing, but know nobody's forcing you. πŸ™ˆ
  • inherit_trace => $bool β€” this one is used for tool-side coderefs.
  • no_fork => $bool β€” react to forking/threading inside the subtest itself. in general you are unlikely to need or want this parameter, it's giving niche. πŸ’€

🌈 SUBTEST WITH TOOL-SIDE CODEREF 🌈

this one is particularly useful if you want to turn a tool that wraps other tools into a subtest. for this we're using the tool we created in Test2::Manual::Tooling::Nesting. full circle moment fr fr.

use Test2::API qw/context run_subtest/;

sub check_class {
    my $class = shift;

    my $ctx = context();

    my $code = sub {
        my $obj = $class->new;
        is($obj->foo, 'foo', "got foo");
        is($obj->bar, 'bar', "got bar");
    };

    my $bool = run_subtest($class, $code, {buffered => 1, inherit_trace => 1});

    $ctx->release;

    return $bool;
}

the run_subtest() function does all the heavy lifting for us β€” main character energy, honestly. all we need to do is hand it a name, a coderef to run, and the inherit_trace => 1 parameter. the buffered => 1 parameter is optional, but recommended. πŸ’…

so what does inherit_trace actually do? it tells the subtest tool that the contexts acquired inside the nested tools should use the same trace as the subtest itself. that's the whole vibe. 🧚

and here's the important bit β€” for user-supplied codeblocks you do not use inherit_trace, because you want errors to report to the user-supplied file+line. iykyk. don't mix these up bestie, that's the whole point. 😭


πŸ‘‘ SEE ALSO πŸ‘‘

  • Test2::Manual β€” Primary index of the manual. the mothership, the main hub, start here. πŸ’œ

πŸ’– SOURCE πŸ’–

the source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. go peek, it's cute. ✨


🌸 MAINTAINERS 🌸

Chad Granum <exodist@cpan.org>


πŸ’… AUTHORS πŸ’…

Chad Granum <exodist@cpan.org>


Copyright Chad Granum <exodist@cpan.org>.

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/


okay that's the whole page bestie, you ate. 🫢

and a lil sign-off for the record books: this was perl v5.44.0, dated 2026-10-17, filed under Test2:...Subtest(3perl). slay, go write that tool. πŸ’–πŸ”₯πŸ‘‘

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms