Test2::Transition(3pm)
π NAME π
Test2::Transition β transition notes when upgrading to Test2, bestie. It's the 3pm page, and it's literally your survival guide. π
πΈ DESCRIPTION πΈ
Okay so this is where the gotchas and breakages related to the Test2 upgrade get documented. The upgrade makes Test::Builder defer to Test2 under the hood. Mostly transparent, but there are a few cases that can trip you up. No cap.
π¦ THINGS THAT BREAK π¦
This is the list of scenarios that break with the new internals. Buckle up, bestie.
Test::Builder1.5/2 conditionals
The Problem
A few years back there were two attempts to upgrade/replace Test::Builder. Confusingly these were called Test::Builder2 and Test::Builder1.5, in that order. Many people put conditionals in their code to check the Test::Builder version number and adapt their code accordingly.
The Test::Builder2/1.5 projects both died out. Now the conditional code people added has become a mine field. A vast majority of modules broken by Test2 fall into this category.
The Fix
The fix is to remove all Test::Builder1.5/2 related code. Either use the legacy Test::Builder API, or use Test2 directly.
Replacing the Test::Builder singleton
The Problem
Some test modules would replace the Test::Builder singleton instance with their own instance or subclass. This was usually done to intercept or modify results as they happened.
The Test::Builder singleton is now a simple compatibility wrapper around Test2. The Test::Builder singleton is no longer the central place for results. Many results bypass the Test::Builder singleton completely, which breaks the behavior intended when replacing the singleton.
The Fix
If you simply want to intercept all results instead of letting them go to TAP, you should look at the Test2::API docs and read about pushing a new hub onto the hub stack. Replacing the hub temporarily is now the correct way to intercept results.
If your goal is purely monitoring of events use the Test2::Hub->listen() method exported by Test::More to watch events as they are fired. If you wish to modify results before they go to TAP look at the Test2::Hub->filter() method.
Directly Accessing Hash Elements
The Problem
Some modules look directly at hash keys on the Test::Builder singleton. The problem here is that the Test::Builder singleton no longer holds anything important.
The Fix
The fix is to use the API specified in Test2::API to look at or modify state as needed.
Subtest indentation
The Problem
An early change β in fact the change that made Test2 an idea β was a change to the indentation of the subtest note. It was decided it would be more readable to outdent the subtest note instead of having it inline with the subtest:
# subtest foo
ok 1 - blah
1..1
ok 1 - subtest foo
The old style indented the note:
# subtest foo
ok 1 - blah
1..1
ok 1 - subtest foo
This breaks tests that do string comparison of TAP output.
The Fix
my $indent = $INC{'Test2/API.pm'} ? '' : ' ';
is(
$subtest_output,
"${indent}# subtest foo",
"Got subtest note"
);
Check if $INC{'Test2/API.pm'} is set, if it is then no indentation should be expected. If it is not set, then the old Test::Builder is in use, indentation should be expected.
π DISTRIBUTIONS THAT BREAK OR NEED TO BE UPGRADED π
This is a list of CPAN modules that have been known to have been broken by the upgrade at one point.
π WORKS BUT TESTS WILL FAIL π
These modules still function correctly, but their test suites will not pass. If you already have these modules installed then you can continue to use them. If you are trying to install them after upgrading Test::Builder you will need to force installation, or bypass the broken tests.
Test::DBIx::Class::Schemaβ This module has a test that appears to work around aTest::Builderbug. The bug appears to have been fixed by Test2, which means the workaround causes a failure. This can be easily updated, but nobody has done so yet.
Known broken in versions: 1.0.9 and older
Device::Chipβ Tests break due to subtest indentation.
Known broken in version 0.07. Apparently works fine in 0.06 though. Patch has been submitted to fix the issue.
β¨ UPGRADE SUGGESTED β¨
These are modules that did not break, but had broken test suites that have since been fixed. Slay.
Test::Exceptionβ Old versions work fine, but have a minor test name behavior that breaks with Test2. Old versions will no longer install because of this. The latest version on CPAN will install just fine. Upgrading is not required, but is recommended.
Fixed in version: 0.43
Data::Peekβ Some tests depended on$!and$?being modified in subtle ways. A patch was applied to correct things that changed.
The module itself works fine, there is no need to upgrade.
Fixed in version: 0.45
circular::requireβ Some tests were fragile and requiredbase.pmto be loaded at a late stage. Test2 was loadingbase.pmtoo early. The tests were updated to fix this.
The module itself never broke, you do not need to upgrade.
Fixed in version: 0.12
Test::Module::Usedβ A test worked around a now-fixed planning bug. There is no need to upgrade if you have an old version installed. New versions install fine if you want them.
Fixed in version: 0.2.5
Test::Moose::Moreβ Some tests were fragile, but have been fixed. The actual breakage was from the subtest comment indentation change.
No need to upgrade, old versions work fine. Only new versions will install.
Fixed in version: 0.025
Test::FITesqueβ This was broken by a bugfix to how planning is done. The test was updated after the bugfix.
Fixed in version: 0.04
Test::Kitβ Old versions work fine, but would not install becauseTest::Aggregatewas in the dependency chain. An upgrade should not be needed.
Fixed in version: 2.15
autouseβ A test broke because it depended onScalar::Utilnot being loaded. Test2 loadsScalar::Util. The test was updated to load Test2 after checkingScalar::Util's load status.
There is no need to upgrade if you already have it installed.
Fixed in version: 1.11
π¨ NEED TO UPGRADE π¨
These ones? Yeah, you actually need to upgrade, bestie.
Test::SharedForkβ Old versions need to directly accessTest::Buildersingleton hash elements. The latest version on CPAN will still do this on oldTest::Builder, but will defer toTest2::IPCon Test2.
Fixed in version: 0.35
Test::Builder::Clutchβ This works by doing overriding methods on the singleton, and directly accessing hash values on the singleton. A new version has been released that uses the Test2 API to accomplish the same result in a saner way.
Fixed in version: 0.07
Test::Dist::VersionSyncβ This hadTest::Builder2conditionals. This was fixed by removing the conditionals.
Fixed in version: 1.1.4
Test::Modernβ This relied onTest::Builder->_try()which was a private method, documented as something nobody should use. This was fixed by using a different tool.
Fixed in version: 0.012
Test::UseAllModulesβ Version0.14relied onTest::Builder->historywhich was available inTest::Builder 1.5. Versions0.12and0.13relied on otherTest::Builderinternals.
Fixed in version: 0.15
Test::More::Prefixβ Worked by applying a role that wrappedTest::Builder->_print_comment. Fixed by adding an event filter that modifies the message instead when running under Test2.
Fixed in version: 0.007
π STILL BROKEN π
These are still giving broken vibes, ngl.
Test::Aggregateβ This distribution directly accesses the hash keys in theTest::Buildersingleton. It also approaches the problem from the wrong angle, please consider usingTest2::Aggregatefor similar functionality andTest2::Harnesswhich allows module preloading at the harness level.
Still broken as of version: 0.373
Test::Wrapperβ This module directly uses hash keys in theTest::Buildersingleton. This module is also obsolete thanks to the benefits of Test2. Useintercept()fromTest2::APIto achieve a similar result.
Still broken as of version: 0.3.0
Test::ParallelSubtestβ This module overridesTest::Builder::subtest()andTest::Builder::done_testing(). It also directly accesses hash elements of the singleton. It has not yet been fixed.
Alternatives: Test2::AsyncSubtest and Test2::Workflow (not stable).
Still broken as of version: 0.05
Test::Prettyβ Seehttps://github.com/tokuhirom/Test-Pretty/issues/25
The author admits the module is crazy, and he is awaiting a stable release of something new (Test2) to completely rewrite it in a sane way.
Still broken as of version: 0.32
Net::BitTorrentβ The tests for this module directly accessTest::Builderhash keys. Most, if not all of these hash keys have public API methods that could be used instead to avoid the problem.
Still broken in version: 0.052
Test::Groupβ It monkeypatchesTest::Builder, and calls it"black magic"in the code.
Still broken as of version: 0.20
Test::Flattenβ This modifies theTest::Builderinternals in many ways. A better way to accomplish the goal of this module is to write your own subtest function.
Still broken as of version: 0.11
Log::Dispatch::Config::TestLogβ ModifiesTest::Builderinternals.
Still broken as of version: 0.02
Test::Ableβ ModifiesTest::Builderinternals.
Still broken as of version: 0.11
π MAKE ASSERTIONS -> SEND EVENTS π
Okay so bestie, this is the glow-up: stop poking the old singleton and start sending events through a context. Itβs giving βsame job, way better vibe.β β¨
π°οΈ LEGACY
use Test::Builder;
# A majority of tools out there do this:
# my $TB = Test::Builder->new;
# This works, but has always been wrong, forcing Test::Builder to implement
# subtests as a horrific hack. It also causes problems for tools that try
# to replace the singleton (also discouraged).
sub my_ok($;$) {
my ($bool, $name) = @_;
my $TB = Test::Builder->new;
$TB->ok($bool, $name);
}
sub my_diag($) {
my ($msg) = @_;
my $TB = Test::Builder->new;
$TB->diag($msg);
}
Like, yes, my $TB = Test::Builder->new; βworks,β but it has always been wrong. It forces Test::Builder to implement subtests as a horrific hack, and it causes problems for tools that try to replace the singleton β which is also discouraged. Soβ¦ yeah. Not iconic. π
β¨ TEST2
use Test2::API qw/context/;
sub my_ok($;$) {
my ($bool, $name) = @_;
my $ctx = context();
$ctx->ok($bool, $name);
$ctx->release;
}
sub my_diag($) {
my ($msg) = @_;
my $ctx = context();
$ctx->diag($msg);
$ctx->release;
}
The context object has API compatible implementations of these methods, bestie:
ok($bool, $name)diag(@messages)note(@messages)subtest($name, $code)
If you are looking for helpers with "is", "like", and others, see Test2::Suite. π«Ά
π WRAP EXISTING TOOLS π
π°οΈ LEGACY
use Test::More;
sub exclusive_ok {
my ($bool1, $bool2, $name) = @_;
# Ensure errors are reported 1 level higher
local $Test::Builder::Level = $Test::Builder::Level + 1;
$ok = $bool1 || $bool2;
$ok &&= !($bool1 && $bool2);
ok($ok, $name);
return $bool;
}
Every single tool in the chain from this, to "ok", to anything "ok" calls needs to increment the $Level variable. When an error occurs, Test::Builder will do a trace to the stack frame determined by $Level, and report that file+line as the one where the error occurred.
β οΈ If you or any other tool you use forgets to set $Level, then errors will be reported to the wrong place. No cap, thatβs a mess. π
β¨ TEST2
use Test::More;
sub exclusive_ok {
my ($bool1, $bool2, $name) = @_;
# Grab and store the context, even if you do not need to use it
# directly.
my $ctx = context();
$ok = $bool1 || $bool2;
$ok &&= !($bool1 && $bool2);
ok($ok, $name);
$ctx->release;
return $bool;
}
Instead of using $Level to perform a backtrace, Test2 uses a context object. In this sample you create a context object and store it. This locks the context β errors report 1 level up from here β for all wrapped tools to find.
You do not need to use the context object, but you do need to store it in a variable. Once the sub ends, the $ctx variable is destroyed, which lets future tools find their own. Main character energy: the context knows when to exit. π
π USING UTF8 π
π°οΈ LEGACY
# Set the mode BEFORE anything loads Test::Builder
use open ':std', ':encoding(utf8)';
use Test::More;
Or:
# Modify the filehandles
my $builder = Test::More->builder;
binmode $builder->output, ":encoding(utf8)";
binmode $builder->failure_output, ":encoding(utf8)";
binmode $builder->todo_output, ":encoding(utf8)";
β¨ TEST2
use Test2::API qw/test2_stack/;
test2_stack->top->format->encoding('utf8');
Though a much better way is to use the Test2::Plugin::UTF8 plugin, which is part of Test2::Suite. Obsessed. π¦
π AUTHORS, CONTRIBUTORS AND REVIEWERS π
The following people have all contributed to this document in some way, even if only for review.
Chad Granum (EXODIST) <exodist@cpan.org>
π¦ SOURCE π¦
The source code repository for Test2 can be found at <https://github.com/Test-More/test-more/>.
π«Ά MAINTAINER π«Ά
Chad Granum <exodist@cpan.org>
Β©οΈ COPYRIGHT Β©οΈ
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 <https://dev.perl.org/licenses/>
And thatβs the whole page, bestie β sourced from perl v5.42.3, dated 2026-11-03. Slay. ππβ¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms