1Test::NoWarnings(3) User Contributed Perl Documentation Test::NoWarnings(3)
2
3
4
6 Test::NoWarnings - Make sure you didn't emit any warnings while testing
7
9 For scripts that have no plan
10
11 use Test::More 'no_plan';
12 use Test::NoWarnings;
13
14 that's it, you don't need to do anything else
15
16 For scripts that look like
17
18 use Test::More tests => x;
19
20 change to
21
22 use Test::More tests => x + 1;
23 use Test::NoWarnings;
24
25 For scripts that use done_testing, use:
26
27 use Test::More;
28 use Test::NoWarnings 'had_no_warnings';
29
30 ... # your actual tests
31
32 had_no_warnings;
33 done_testing;
34
36 In general, your tests shouldn't produce warnings. This modules causes
37 any warnings to be captured and stored. It automatically adds an extra
38 test that will run when your script ends to check that there were no
39 warnings. If there were any warnings, the test will give a "not ok" and
40 diagnostics of where, when and what the warning was, including a stack
41 trace of what was going on when the it occurred.
42
43 If some of your tests are supposed to produce warnings then you should
44 be capturing and checking them with Test::Warn, that way
45 Test::NoWarnings will not see them and so not complain.
46
47 The test is run by an "END" block in Test::NoWarnings. It will not be
48 run when any forked children exit.
49
51 Simply by using the module, you automatically get an extra test at the
52 end of your script that checks that no warnings were emitted. So just
53 stick
54
55 use Test::NoWarnings;
56
57 at the top of your script and continue as normal.
58
59 If you want more control you can invoke the test manually at any time
60 with "had_no_warnings".
61
62 The warnings your test has generated so far are stored in an array. You
63 can look inside and clear this whenever you want with "warnings()" and
64 "clear_warnings", however, if you are doing this sort of thing then you
65 probably want to use Test::Warn in combination with Test::NoWarnings.
66
67 If you have a test script written using done_testing and no test plan,
68 you have to call "had_no_warnings" before you call done_testing.
69
70 use vs require
71 You will almost always want to do
72
73 use Test::NoWarnings;
74
75 If you do a "require" rather than a "use", then there will be no
76 automatic test at the end of your script.
77
78 Output
79 If warning is captured during your test then the details will output as
80 part of the diagnostics. You will get:
81
82 • the number and name of the test that was executed just before the
83 warning (if no test had been executed these will be 0 and '')
84
85 • the message passed to "warn",
86
87 • a full dump of the stack when warn was called, courtesy of the "Carp"
88 module
89
90 By default, all warning messages will be emitted in one block at the
91 end of your test script.
92
93 The :early pragma
94 One common complaint from people using Test::NoWarnings is that all of
95 the warnings are emitted in one go at the end. While this is the safest
96 and most correct time to emit these diagnostics, it can make debugging
97 these warnings difficult.
98
99 As of Test::NoWarnings 1.04 you can provide an experimental ":early"
100 pragma when loading the module to force warnings to be thrown via diag
101 at the time that they actually occur.
102
103 use Test::NoWarnings ':early';
104
105 As this will cause the diag to be emitted against the previous test and
106 not the one in which the warning actually occurred it is recommended
107 that the pragma be turned on only for debugging and left off when not
108 needed.
109
111 had_no_warnings
112 This checks that there have been warnings emitted by your test scripts.
113 Usually you will not call this explicitly as it is called automatically
114 when your script finishes.
115
116 clear_warnings
117 This will clear the array of warnings that have been captured. If the
118 array is empty then a call to "had_no_warnings()" will produce a pass
119 result.
120
121 warnings
122 This will return the array of warnings captured so far. Each element of
123 this array is an object containing information about the warning. The
124 following methods are available on these object.
125
126 • $warn->getMessage
127
128 Get the message that would been printed by the warning.
129
130 • $warn->getCarp
131
132 Get a stack trace of what was going on when the warning happened,
133 this stack trace is just a string generated by the Carp module.
134
135 • $warn->getTrace
136
137 Get a stack trace object generated by the Devel::StackTrace module.
138 This will return undef if Devel::StackTrace is not installed.
139
140 • $warn->getTest
141
142 Get the number of the test that executed before the warning was
143 emitted.
144
145 • $warn->getTestName
146
147 Get the name of the test that executed before the warning was
148 emitted.
149
151 When counting your tests for the plan, don't forget to include the test
152 that runs automatically when your script ends.
153
154 Checking for warnings is done using $SIG{__WARN__}. If other modules
155 use this hook, it can interfere with this module's operation. For
156 example, using the Carp::Always module will cause this module to always
157 report no warnings.
158
160 Bugs should be reported via the CPAN bug tracker at
161
162 <https://rt.cpan.org/Ticket/Create.html?Queue=Test-NoWarnings>
163
164 For other issues, contact the author.
165
167 This was previously known as Test::Warn::None
168
170 Test::Builder, Test::Warn
171
173 Fergal Daly <fergal@esatclear.ie>
174
175 Adam Kennedy <adamk@cpan.org>
176
178 Copyright 2003 - 2007 Fergal Daly.
179
180 Some parts copyright 2010 - 2011 Adam Kennedy.
181
182 This program is free software and comes with no warranty. It is
183 distributed under the LGPL license
184
185 See the file LICENSE included in this distribution or
186 http://www.fsf.org/licenses/licenses.html.
187
188
189
190perl v5.34.0 2021-08-03 Test::NoWarnings(3)