++ed by:

13 non-PAUSE users.

Karen Etheridge 🐾 🌋


Test::Warnings - Test for warnings and the lack of them


version 0.001


    use Test::More;
    use Test::Warnings;


emits TAP:

    ok 1 - yay!
    ok 2 - no (unexpected) warnings (via done_testing)


If you've ever tried to use Test::NoWarnings to confirm there are no warnings generated by your tests, combined with the convenience of done_testing to not have to declare a test count, you'll have discovered that these two features do not play well together, as the test count will be calculated before the warnings test is run, resulting in a TAP error. (See examples/test_nowarnings.pl in this distribution for a demonstration.)

This module is intended to be used as a drop-in replacement for Test::NoWarnings: it also adds an extra test, but runs this test before done_testing calculates the test count, rather than after. It does this by hooking into done_testing as well as via an END block. You can declare a plan, or not, and things will still Just Work.

It is actually equivalent to:

    use Test::NoWarnings 1.04 ':early';

as warnings are still printed normally as they occur. You are safe, and enthusiastically encouraged, to perform a global search-replace of the above with use Test::Warnings; whether or not your tests have a plan.


The following functions are available for import (not included by default):

  • allow_warnings([bool])

    When passed a true value, or no value at all, subsequent warnings will not result in a test failure; when passed a false value, subsequent warnings will result in a test failure. Initial value is false.

  • allowing_warnings

    Returns whether we are currently allowing warnings (set by allow_warnings as described above).

  • had_no_warnings(<optional test name>)

    Tests whether there have been any warnings so far, not preceded by an allowing_warnings call. It is run automatically at the end of all tests, but can also be called manually at any time, as often as desired.

All functions are also available by importing the tag :all.


  • allow_warnings(qr/.../) - allow some warnings and not others

  • warning_is, warning_like etc... - inclusion of some Test::Warn-like functionality for testing the content of warnings, but closer to a Test::Fatal-like syntax

  • more sophisticated handling in subtests - if we save some state on the Test::Builder object itself, we can allow warnings in a subtest and then the state will revert when the subtest ends, as well as check for warnings at the end of every subtest via done_testing.


Bugs may be submitted through https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Warning. I am also usually active on irc, as 'ether' at irc.perl.org.





Karen Etheridge <ether@cpan.org>


This software is copyright (c) 2013 by Karen Etheridge.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.