Test::NoWarnings - Make sure you didn't emit any warnings while testing
For scripts that have no plan
use Test::More 'no_plan';
use Test::NoWarnings;
that's it, you don't need to do anything else
For scripts that look like
use Test::More tests => x;
change to
use Test::More tests => x + 1;
use Test::NoWarnings;
For scripts that use done_testing, use:
use Test::More;
use Test::NoWarnings 'had_no_warnings';
... # your actual tests
had_no_warnings;
done_testing;
In general, your tests shouldn't produce warnings. This modules causes any
warnings to be captured and stored. It automatically adds an extra test that
will run when your script ends to check that there were no warnings. If
there were any warnings, the test will give a ``not ok'' and diagnostics of
where, when and what the warning was, including a stack trace of what was
going on when the it occurred.
If some of your tests are supposed to produce warnings then you should be
capturing and checking them with the Test::Warn manpage, that way the Test::NoWarnings manpage
will not see them and so not complain.
The test is run by an END block in Test::NoWarnings. It will not be run
when any forked children exit.
Simply by using the module, you automatically get an extra test at the end
of your script that checks that no warnings were emitted. So just stick
use Test::NoWarnings;
at the top of your script and continue as normal.
If you want more control you can invoke the test manually at any time with
had_no_warnings .
The warnings your test has generated so far are stored in an array. You can
look inside and clear this whenever you want with warnings() and
clear_warnings , however, if you are doing this sort of thing then you
probably want to use the Test::Warn manpage in combination with the Test::NoWarnings manpage.
If you have a test script written using done_testing
and no test plan, you have to call had_no_warnings before you call
done_testing.
You will almost always want to do
use Test::NoWarnings;
If you do a require rather than a use , then there will be no automatic
test at the end of your script.
If warning is captured during your test then the details will output as part
of the diagnostics. You will get:
-
the number and name of the test that was executed just before the warning
(if no test had been executed these will be 0 and '')
-
the message passed to
warn ,
-
a full dump of the stack when warn was called, courtesy of the
Carp
module
By default, all warning messages will be emitted in one block at the end of
your test script.
One common complaint from people using Test::NoWarnings is that all of the
warnings are emitted in one go at the end. While this is the safest and
most correct time to emit these diagnostics, it can make debugging these
warnings difficult.
As of Test::NoWarnings 1.04 you can provide an experimental :early pragma
when loading the module to force warnings to be thrown via diag at the time
that they actually occur.
use Test::NoWarnings ':early';
As this will cause the diag to be emitted against the previous test and not
the one in which the warning actually occurred it is recommended that the
pragma be turned on only for debugging and left off when not needed.
This checks that there have been warnings emitted by your test scripts.
Usually you will not call this explicitly as it is called automatically when
your script finishes.
This will clear the array of warnings that have been captured. If the array
is empty then a call to had_no_warnings() will produce a pass result.
This will return the array of warnings captured so far. Each element of this
array is an object containing information about the warning. The following
methods are available on these object.
-
$warn->getMessage
Get the message that would been printed by the warning.
-
$warn->getCarp
Get a stack trace of what was going on when the warning happened, this stack
trace is just a string generated by the Carp module.
-
$warn->getTrace
Get a stack trace object generated by the the Devel::StackTrace manpage module. This
will return undef if the Devel::StackTrace manpage is not installed.
-
$warn->getTest
Get the number of the test that executed before the warning was emitted.
-
$warn->getTestName
Get the name of the test that executed before the warning was emitted.
When counting your tests for the plan, don't forget to include the test that
runs automatically when your script ends.
Checking for warnings is done using $SIG{__WARN__} .
If other modules use this hook, it can interfere with this module's operation.
For example, using the the Carp::Always manpage module will cause this module to always
report no warnings.
Bugs should be reported via the CPAN bug tracker at
https://rt.cpan.org/Ticket/Create.html?Queue=Test-NoWarnings
For other issues, contact the author.
This was previously known as the Test::Warn::None manpage
the Test::Builder manpage, the Test::Warn manpage
Fergal Daly <fergal@esatclear.ie>
Adam Kennedy <adamk@cpan.org>
Copyright 2003 - 2007 Fergal Daly.
Some parts copyright 2010 - 2011 Adam Kennedy.
This program is free software and comes with no warranty. It is distributed
under the LGPL license
See the file LICENSE included in this distribution or
http://www.fsf.org/licenses/licenses.html.
|