Test2::Require(3pm)

πŸ’– NAME πŸ’–

okay so this is Test2::Require(3pm), straight outta the Perl Programmers Reference Guide β€” and Test2::Require is literally the base class and documentation for skip-unless type test packages. periodt. πŸ’…

🌸 DESCRIPTION 🌸

Test2::Require::* packages are packages you load to ensure your test file is skipped unless a specific requirement is met. That's the whole point: no requirement, no test run. 🫢

Modules in this namespace may subclass Test2::Require if they wish, but it is not strictly necessary to do so. lowkey flexible, fr fr. ✨

πŸŽ€ HOW DO I WRITE A 'REQUIRE' MODULE? πŸŽ€

🧚 AS A SUBCLASS 🧚

here's the subclass route, bestie β€” copy this energy:

package Test2::Require::Widget;
use strict;
use warnings;

use base 'Test2::Require';

sub HAVE_WIDGETS { ... };

sub skip {
    my $class = shift;
    my @import_args = @_;

    if (HAVE_WIDGETS()) {
        # We have widgets, do not skip
        return undef;
    }
    else {
        # No widgets, skip the test
        return "Skipped because there are no widgets" unless HAVE_WIDGETS();
    }
}

1;

A subclass of Test2::Require simply needs to implement a skip() method. That method will receive all import arguments. It should return undef if the test should run, and it should return a reason for skipping if the test should be skipped. no cap. πŸ’–

πŸ¦‹ STAND-ALONE πŸ¦‹

If you do not wish to subclass Test2::Require then you should write an import() method:

package Test2::Require::Widget;
use strict;
use warnings;

use Test2::API qw/context/;

sub HAVE_WIDGETS { ... };

sub import {
    my $class = shift;

    # Have widgets, should run.
    return if HAVE_WIDGETS();

    # Use the context object to create the event
    my $ctx = context();
    $ctx->plan(0, SKIP => "Skipped because there are no widgets");
    $ctx->release;
}

1;

that's the stand-alone vibe β€” context object, skip plan, release, done. iconic. πŸ’…

πŸŽ€ 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/


and that's the page, bestie 🫢 β€” this was Test2::Require(3pm) from perl v5.42.3, dated 2026-11-03. go make skip-unless magic that slays πŸ’…βœ¨

πŸ“¦ where this page comes from