Test::NoWarnings - Make sure you didn't emit any warnings while testing |
Test::NoWarnings - Make sure you didn't emit any warnings while testing
For scripts that have 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;
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 warings, 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.
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:
warn
,
Carp
module
had_no_warnings()
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.
clear_warnings()
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.
warnings()
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.
Get the message that would been printed by the warning.
$warn->getCarpGet a stack trace of what was going on when the warning happened, this stack trace is just a string generated by the the Carp manpage module.
$warn->getTraceGet 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->getTestGet the number of the test that executed before the warning was emitted.
$warn->getTestNameGet 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.
None that I know of.
This was previously known as the Test::Warn::None manpage
the Test::Builder manpage, the Test::Warn manpage
Written by Fergal Daly <fergal@esatclear.ie>.
Copyright 2003 by Fergal Daly <fergal@esatclear.ie>.
This program is free software and comes with no warranty. It is distributed under the LGPL license
See the file LGPL included in this distribution or http://www.fsf.org/licenses/licenses.html.
Test::NoWarnings - Make sure you didn't emit any warnings while testing |