Test::S...arnings(3)

πŸ“¦
version 1.302027 Β· 2 versions
, in fedora 43

✨ Test::Stream::Plugin::Warnings ✨

okay bestie, so this is a page from the User Contributed Perl Documentation collection, and it's all about Test::Stream::Plugin::Warnings πŸ’– β€” the lil plugin that gives you simple tools for testing code that may generate warnings. no cap, that's the whole NAME vibe.


πŸ’€ DEPRECATED πŸ’€

ngl this one's giving "we need to talk." This distribution is deprecated in favor of Test2, Test2::Suite, and Test2::Workflow. 🫢

There's a whole conversion guide waiting for you at Test::Stream::Manual::ToTest2 β€” go read it, it's the main character of your migration arc. 🌈


🌸 SYNOPSIS 🌸

# Load the Warnings plugin, and Core cause we need that as well.
use Test::Stream qw/Core Warnings/;

# Returns undef if there are no warnings.
ok(!warns { ... }, "Codeblock did not warn");

is_deeply(
    warns { warn "foo\n"; warn "bar\n" },
    [
        "foo\n",
        "bar\n",
    ],
    "Got expected warnings"
);

# Dies if there are 0 warnings, or 2+ warnings, otherwise returns the 1 warning.
like( warning { warn 'xxx' }, qr/xxx/, "Got expected warning");

πŸŽ€ EXPORTS πŸŽ€

  • $warnings = warns { ... } β€” bestie, if the codeblock issues any warnings, they get captured and handed back to you in an arrayref. πŸ’… If there were no warnings this returns undef. And here's the tea: you can be sure this will always be undef, or an arrayref with 1 or more warnings. no in-between, it's not messy like that.
# Returns undef if there are no warnings.
ok(!warns { ... }, "Codeblock did not warn");

is_deeply(
    warns { warn "foo\n"; warn "bar\n" },
    [
        "foo\n",
        "bar\n",
    ],
    "Got expected warnings"
);
  • $warning = warning { ... } β€” only use this one for code that should issue exactly 1 warning. periodt. It will throw an exception if there are no warnings, or if there are multiple warnings. it does not do gray areas, it does not do "eh close enough." 🚨
like( warning { warn 'xxx' }, qr/xxx/, "Got expected warning");

These both die, fr fr:

warning { warn 'xxx'; warn 'yyy' };
warning { return };
  • $bool = no_warnings { ... } β€” this returns true if there are no warnings in the codeblock. 🌟 It returns false, and prints the warnings, if any are encountered. so you get the receipts.
ok(no_warnings { ... }, "Did not warn.");

Honestly this is sometimes more useful than checking !warns { ... } since it lets you see the warnings when it fails. it's giving "show your work." 😭


πŸ”₯ SOURCE πŸ”₯

The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/. go lurk, it's iconic. πŸ‘‘


πŸ‘‘ MAINTAINERS πŸ‘‘

  • Chad Granum <exodist@cpan.org>

🌟 AUTHORS 🌟

  • Chad Granum <exodist@cpan.org>

Copyright 2015 Chad Granum <exodist7@gmail.com>.

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 a wrap, bestie! πŸ¦‹ this page shipped as perl v5.42.0, dated 2025-07-25 β€” slay, go write some tests that actually check for warnings πŸ’–βœ¨πŸ§š
πŸ“¦ where this page comes from