Test::Class::Tiny(3)
β¨ NAME β¨
okay bestie, welcome to the Test::Class::Tiny page, section 3 of the User Contributed Perl Documentation β aka xUnit in Perl, simplified π
β¨
π SYNOPSIS π
package t::mytest;
use parent qw( Test::Class::Tiny );
__PACKAGE__->runtests() if !caller;
sub T_startup_something {
# Runs at the start of the test run.
}
sub something_T_setup {
# Runs before each normal test function
}
# Expects 2 assertions:
sub T2_normal {
ok(1, 'yes');
ok( !0, 'no');
}
# Ignores assertion count:
sub T0_whatever {
ok(1, 'yes');
}
sub T_teardown_something {
# Runs after each normal test function
}
sub T_shutdown_something {
# Runs at the end of the test run.
}
π STATUS π
bestie, this module is EXPERIMENTAL. If you use it, you MUST check the changelog before upgrading to a new version. Any CPAN distributions that use this module could break whenever this module is updated. no cap, fr fr. π
π¦ DESCRIPTION π¦
Test::Class has served Perlβs xUnit needs for a long time but is incompatible with the Test2 framework. This module allows for a similar workflow but in a way that works with both Test2 and the older, Test::Builder-based modules. slay. π
πΈ HOW (AND WHY) TO USE THIS MODULE πΈ
xUnit encourages well-designed tests by encouraging organization of test logic into independent chunks of test logic rather than a single monolithic block of code. itβs giving structure, and Iβm obsessed. β¨
xUnit provides standard hooks for:
- startup: The start of all tests
- setup: The start of an individual test group (i.e., Perl function)
- teardown: The end of an individual test group
- shutdown: The end of all tests
To write functions that execute at these points in the workflow, name those functions with the prefixes T_startup_, T_setup_, T_teardown_, or T_shutdown_. Alternatively, name such functions with the suffixes _T_startup, _T_setup, _T_teardown, or _T_shutdown. pick your vibe, bestie. π«Ά
To write a test functionβi.e., a function that actually runs some assertionsβprefix the function name with T, the number of test assertions in the function, then an underscore. For example, a function that contains 9 assertions might be named T9_check_validation(). If that function doesnβt run exactly 9 assertions, a test failure is produced. the count is literally the whole point, iykyk. π₯
To forgo counting test assertions, use 0 as the test count, e.g., T0_check_validation().
You may alternatively use suffix-style naming for test functions well, e.g., check_validation_T9(), check_validation_T0().
The above convention is a significant departure from Test::Class, which uses Perl subroutine attributes to indicate this information. Using method names is dramatically simpler to implement and also easier to type. ngl, typing less is main character energy. π
In most other respects this module attempts to imitate Test::Class.
π PLANS π
The concept of a global βplanβ (i.e., an expected number of assertions) isnβt all that sensible with xUnit because each test function has its own plan. So, ideally the total number of expected assertions for a given test module is just the sum of all test functionsβ expected assertions. math, but make it test-y. β¨
Thus, currently, runtests() sets the Test2::Hub objectβs plan to "no_plan" if the plan is undefined.
π TEST INHERITANCE π
Like Test::Class, this module seamlessly integrates inherited methods. To have one test module inherit another moduleβs tests, just make that first module a subclass of the latter. family tree, but for tests. π§
CAVEAT EMPTOR: Inheritance in tests, while occasionally useful, can also make for difficult maintenance over time if overused. Where Iβve found it most useful is cases like Promise::ES6, where each test needs to run with each backend implementation. iconic when used right, chaotic when overdone. π
π RUNNING YOUR TEST π
To use this module to write normal Perl test scripts, just define the scriptβs package (ideally not main, but itβll work) as a subclass of this module. Then put the following somewhere in the script:
__PACKAGE__->runtests() if !caller;
Your test will thus execute as a βmodulinoβ. modulino is such a word. periodt. π
π SPECIAL FEATURES π
- As in
Test::Class, aSKIP_CLASS()method may be defined. If this method returns truthy, then the classβs tests are skipped, and that truthy return is given as the reason for the skip. - The
"TEST_METHOD"environment variable is honored as inTest::Class. Test::Classβsfail_if_returned_early()method is NOT recognized here because an early return will already trigger a failure. do not come for that method, bestie. π- Within a test method,
num_tests()may be called to retrieve the number of expected test assertions. - To define a test function whose test count isnβt known until runtime, name it without the usual
T$numprefix, then at runtime do:
$test_obj->num_method_tests( $name, $count )
See t/ in the distribution for an example of this. runtime surprises, but consensual. β¨
β COMMON PITFALLS β
Avoid the following:
- Writing startup logic outside of the module class, e.g.:
if (!caller) {
my $mock = Test::MockModule->new('Some::Module');
$mock->redefine('somefunc', sub { .. } );
__PACKAGE__->runtests();
}
The above works only if the test module runs in its own process; if you try to run this module with anything else itβll fail because caller() will be truthy, which will prevent the mocking from being set up, which your test probably depends on. not the vibe check you want to fail. π
Instead of the above, write a wrapper around runtests(), thus:
sub runtests {
my $self = shift;
my $mock = Test::MockModule->new('Some::Module');
$mock->redefine('somefunc', sub { .. } );
$self->SUPER::runtests();
}
This ensures your test module will always run with the intended mocking. slay, wrapper. π«Ά
- REDUX: Writing startup logic outside of the module class, e.g.:
my $mock = Test::MockModule->new('Some::Module');
$mock->redefine('somefunc', sub { .. } );
__PACKAGE__->runtests() if !caller;
This is even worse than before because the mock will be global, which will quietly apply it where we donβt intend. This produces action-at-a-distance bugs, which can be notoriously hard to find. highkey cursed. do not do this. π
π₯ SEE ALSO π₯
Besides Test::Class, you might also look at the following:
Test2::Tools::xUnitalso implements xUnit for Test2 but doesnβt allow inheritance.Test::Class::Mooseworks with Test2, but the Moose requirement makes use in CPAN modules problematic.
π AUTHOR π
Copyright 2019 Gasper Software Consulting <http://gaspersoftware.com> (FELIPE) π
π LICENSE π
This code is licensed under the same license as Perl itself. clean, simple, legal. β¨
this page came from perl v5.42.2, dated 2026-02-15, and itβs the Test::Class::Tiny(3) manual page β bye bestie π«Άπ
π¦ where this page comes from
- alpine 3.24: perl-test-class-tiny-doc 0.03-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms