TEST2::TOOLS::SUBTEST(3pm)

✨ NAME ✨

Test2::Tools::Subtest β€” tools for writing subtests, bestie πŸ’–

This is the Test2::Tools::Subtest page from the Perl Programmers Reference Guide, and it is literally all about making subtests happen. Periodt.


πŸ¦‹ DESCRIPTION πŸ¦‹

This package exports subs that let you write subtests. No cap, that is the whole vibe.

There are two types of subtests: buffered and streamed. Streamed subtests mimic subtests from Test::More in that they render all events as soon as they are produced. Buffered subtests wait until the subtest completes before rendering any results.

The main difference is that streamed subtests are unreadable when combined with concurrency. Buffered subtests look fine with any number of concurrent threads and processes. Slay for buffered, honestly.


🌸 SYNOPSIS 🌸

BUFFERED

use Test2::Tools::Subtest qw/subtest_buffered/;

subtest_buffered my_test => sub {
    ok(1, "subtest event A");
    ok(1, "subtest event B");
};

This will produce output like this:

ok 1 - my_test {
    ok 1 - subtest event A
    ok 2 - subtest event B
    1..2
}

STREAMED

Quick reminder bestie: the default option is 'buffered'. If you want streamed subtests, the way Test::Builder does it, use this:

use Test2::Tools::Subtest qw/subtest_streamed/;

subtest_streamed my_test => sub {
    ok(1, "subtest event A");
    ok(1, "subtest event B");
};

This will produce output like this:

# Subtest: my_test
    ok 1 - subtest event A
    ok 2 - subtest event B
    1..2
ok 1 - Subtest: my_test

⚠️ IMPORTANT NOTE ⚠️

You can use bail_out or skip_all in a subtest, but not in a BEGIN block or use statement. This is due to the way flow control works within a BEGIN block. This is not normally an issue, but can happen in rare conditions using eval, or script files as subtests.

Like, it is usually fine, but do not come for me when the rare condition hits. πŸ™ˆ


πŸ’– EXPORTS πŸ’–

subtest_streamed

subtest_streamed $name => $sub
subtest_streamed($name, $sub, @args)
subtest_streamed $name => \%params, $sub
subtest_streamed($name, \%params, $sub, @args)

Run subtest coderef, stream events as they happen. It is giving live commentary energy ✨

\%params is a hashref with any arguments you wish to pass into hub construction.

subtest_buffered

subtest_buffered $name => $sub
subtest_buffered($name, $sub, @args)
subtest_buffered $name => \%params, $sub
subtest_buffered($name, \%params, $sub, @args)

Run subtest coderef, render events all at once when subtest is complete. Very β€œsave it all for the reveal” behavior πŸ’…

\%params is a hashref with any arguments you wish to pass into hub construction.


πŸ”— SOURCE πŸ”—

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


πŸ‘‘ 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/


This edition is perl v5.42.3, dated 2026-11-03 β€” bye bestie, go write some iconic subtests! πŸ’œπŸ«Ά

πŸ“¦ where this page comes from