4 package Test::NoWarnings;
8 use Test::NoWarnings::Warning;
10 my $Test = Test::Builder->new;
16 $VERSION @EXPORT_OK @ISA $do_end_test
22 @ISA = qw( Exporter );
25 clear_warnings had_no_warnings warnings
30 $SIG{__WARN__} = make_catcher(\@warnings);
38 goto &Exporter::import;
41 # the END block must be after the "use Test::Builder" to make sure it runs
42 # before Test::Builder's end block
43 # only run the test if there have been other tests
45 had_no_warnings() if $do_end_test;
54 my $warning = Test::NoWarnings::Warning->new;
56 $warning->setMessage($msg);
57 $warning->fillTest($Test);
58 $warning->fillTrace(__PACKAGE__);
60 $Carp::Internal{__PACKAGE__.""}++;
61 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
62 $warning->fillCarp($msg);
63 $Carp::Internal{__PACKAGE__.""}--;
70 # this make a subroutine which can be used in $SIG{__WARN__}
71 # it takes one argument, a ref to an array
72 # it will push the details of the warning onto the end of the array.
79 $Carp::Internal{__PACKAGE__.""}++;
80 push(@$array, make_warning($msg));
81 $Carp::Internal{__PACKAGE__.""}--;
89 return 0 if $$ != $PID;
92 my $name = shift || "no warnings";
103 $diag = "There were ".@warnings." warning(s)\n";
104 $diag .= join("----------\n", map { $_->toString } @warnings);
107 $Test->ok($ok, $name) || $Test->diag($diag);
114 local $SIG{__WARN__};
120 local $SIG{__WARN__};
126 local $SIG{__WARN__};
140 Test::NoWarnings - Make sure you didn't emit any warnings while testing
144 For scripts that have no plan
146 use Test::NoWarnings;
148 that's it, you don't need to do anything else
150 For scripts that look like
152 use Test::More tests => x;
156 use Test::More tests => x + 1;
157 use Test::NoWarnings;
161 In general, your tests shouldn't produce warnings. This modules causes any
162 warnings to be captured and stored. It automatically adds an extra test that
163 will run when your script ends to check that there were no warnings. If
164 there were any warings, the test will give a "not ok" and diagnostics of
165 where, when and what the warning was, including a stack trace of what was
166 going on when the it occurred.
168 If some of your tests B<are supposed to> produce warnings then you should be
169 capturing and checking them with L<Test::Warn>, that way L<Test::NoWarnings>
170 will not see them and so not complain.
172 The test is run by an END block in Test::NoWarnings. It will not be run when
173 any forked children exit.
177 Simply by using the module, you automatically get an extra test at the end
178 of your script that checks that no warnings were emitted. So just stick
182 at the top of your script and continue as normal.
184 If you want more control you can invoke the test manually at any time with
185 C<had_no_warnings()>.
187 The warnings your test has generated so far are stored in an array. You can
188 look inside and clear this whenever you want with C<warnings()> and
189 C<clear_warnings()>, however, if you are doing this sort of thing then you
190 probably want to use L<Test::Warn> in combination with L<Test::NoWarnings>.
192 =head1 USE vs REQUIRE
194 You will almost always want to do
198 If you do a C<require> rather than a C<use>, then there will be no automatic
199 test at the end of your script.
203 If warning is captured during your test then the details will output as part
204 of the diagnostics. You will get:
210 the number and name of the test that was executed just before the warning
211 (if no test had been executed these will be 0 and '')
215 the message passed to C<warn>,
219 a full dump of the stack when warn was called, courtesy of the C<Carp>
224 =head1 EXPORTABLE FUNCTIONS
226 =head2 had_no_warnings()
228 This checks that there have been warnings emitted by your test scripts.
229 Usually you will not call this explicitly as it is called automatically when
230 your script finishes.
232 =head2 clear_warnings()
234 This will clear the array of warnings that have been captured. If the array
235 is empty then a call to C<had_no_warnings()> will produce a pass result.
239 This will return the array of warnings captured so far. Each element of this
240 array is an object containing information about the warning. The following
241 methods are available on these object.
247 $warn-E<gt>getMessage
249 Get the message that would been printed by the warning.
255 Get a stack trace of what was going on when the warning happened, this stack
256 trace is just a string generated by the L<Carp> module.
262 Get a stack trace object generated by the L<Devel::StackTrace> module. This
263 will return undef if L<Devel::StackTrace> is not installed.
269 Get the number of the test that executed before the warning was emitted.
273 $warn-E<gt>getTestName
275 Get the name of the test that executed before the warning was emitted.
281 When counting your tests for the plan, don't forget to include the test that
282 runs automatically when your script ends.
290 This was previously known as L<Test::Warn::None>
294 L<Test::Builder>, L<Test::Warn>
298 Written by Fergal Daly <fergal@esatclear.ie>.
302 Copyright 2003 by Fergal Daly E<lt>fergal@esatclear.ieE<gt>.
304 This program is free software and comes with no warranty. It is distributed
305 under the LGPL license
307 See the file F<LGPL> included in this distribution or
308 F<http://www.fsf.org/licenses/licenses.html>.