1Warn(3) User Contributed Perl Documentation Warn(3)
2
3
4
6 Test::Warn - Perl extension to test methods for warnings
7
9 use Test::Warn;
10
11 warning_is {foo(-dri => "/")} "Unknown Parameter 'dri'", "dri != dir gives warning";
12 warnings_are {bar(1,1)} ["Width very small", "Height very small"];
13
14 warning_is {add(2,2)} undef, "No warnings for calc 2+2"; # or
15 warnings_are {add(2,2)} [], "No warnings for calc 2+2"; # whichever reads better :-)
16
17 warning_like {foo(-dri => "/")} qr/unknown param/i, "an unknown parameter test";
18 warnings_like {bar(1,1)} [qr/width.*small/i, qr/height.*small/i];
19
20 warning_is {foo()} {carped => "didn't find the right parameters"};
21 warnings_like {foo()} [qr/undefined/,qr/undefined/,{carped => qr/no result/i}];
22
23 warning_like {foo(undef)} 'uninitialized';
24 warning_like {bar(file => '/etc/passwd')} 'io';
25
26 warning_like {eval q/"$x"; $x;/}
27 [qw/void uninitialized/],
28 "some warnings at compile time";
29
30 warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
31
33 A good style of Perl programming calls for a lot of diverse regression
34 tests.
35
36 This module provides a few convenience methods for testing warning
37 based-code.
38
39 If you are not already familiar with the Test::More manpage now would
40 be the time to go take a look.
41
42 FUNCTIONS
43 warning_is BLOCK STRING, TEST_NAME
44 Tests that BLOCK gives the specified warning exactly once.
45
46 The test fails if the BLOCK warns more than once or does not warn
47 at all. If the string is undef, then the test succeeds if the
48 BLOCK doesn't give any warning.
49
50 Another way to say that there are no warnings in the block is:
51
52 warnings_are {foo()} [], "no warnings"
53
54 If you want to test for a warning given by Carp you have to write
55 something like:
56
57 warning_is {carp "msg"} {carped => 'msg'}, "Test for a carped warning";
58
59 The test will fail if a "normal" warning is found instead of a
60 "carped" one.
61
62 Note: "warn "foo"" would print something like "foo at -e line 1".
63 This method ignores everything after the "at". Thus to match this
64 warning you would have to call "warning_is {warn "foo"} "foo", "Foo
65 succeeded"". If you need to test for a warning at an exact line,
66 try something like:
67
68 warning_like {warn "foo"} qr/at XYZ.dat line 5/
69
70 Warn messages with a trailing newline (like "warn "foo\n"") don't
71 produce the "at -e line 1" message by Perl. Up to Test::Warn 0.30
72 such warning weren't supported by "warning_is {warn "foo\n"}
73 "foo\n"". Starting with version 0.31 they are supported, but also
74 marked as experimental.
75
76 warning_is() and warnings_are() are only aliases to the same
77 method. So you also could write "warning_is {foo()} [], "no
78 warning"" or something similar.
79
80 I decided to give two methods the same name to improve readability.
81
82 A true value is returned if the test succeeds, false otherwise.
83
84 The test name is optional, but recommended.
85
86 warnings_are BLOCK ARRAYREF, TEST_NAME
87 Tests to see that BLOCK gives exactly the specified warnings. The
88 test fails if the warnings from BLOCK are not exactly the ones in
89 ARRAYREF. If the ARRAYREF is equal to "[]", then the test succeeds
90 if the BLOCK doesn't give any warning.
91
92 Please read also the notes to warning_is() as these methods are
93 only aliases.
94
95 If you want more than one test for carped warnings, try this:
96
97 warnings_are {carp "c1"; carp "c2"} {carped => ['c1','c2'];
98
99 or
100
101 warnings_are {foo()} ["Warning 1", {carped => ["Carp 1", "Carp 2"]}, "Warning 2"];
102
103 Note that "{carped => ...}" must always be a hash ref.
104
105 warning_like BLOCK REGEXP, TEST_NAME
106 Tests that BLOCK gives exactly one warning and it can be matched by
107 the given regexp.
108
109 If the string is undef, then the tests succeeds if the BLOCK
110 doesn't give any warning.
111
112 The REGEXP is matched against the whole warning line, which in
113 general has the form "WARNING at __FILE__ line __LINE__". So you
114 can check for a warning in the file "Foo.pm" on line 5 with:
115
116 warning_like {bar()} qr/at Foo.pm line 5/, "Testname"
117
118 I don't know whether it makes sense to do such a test :-(
119
120 However, you should be prepared as a matching with 'at', 'file',
121 '\d' or similar will always pass.
122
123 Consider "qr/^foo/" if you want to test for warning "foo something"
124 in file foo.pl.
125
126 You can also write the regexp in a string as "/.../" instead of
127 using the "qr/.../" syntax.
128
129 Note that the slashes are important in the string, as strings
130 without slashes are reserved for warning categories (to match
131 warning categories as can be seen in the perllexwarn man page).
132
133 Similar to warning_is() and warnings_are() you can test for
134 warnings via "carp" with:
135
136 warning_like {bar()} {carped => qr/bar called too early/i};
137
138 Similar to warning_is() and warnings_are(),
139
140 warning_like() and warnings_like() are only aliases to the same
141 methods.
142
143 A true value is returned if the test succeeds, false otherwise.
144
145 The test name is optional, but recommended.
146
147 warning_like BLOCK STRING, TEST_NAME
148 Tests whether a BLOCK gives exactly one warning of the passed
149 category.
150
151 The categories are grouped in a tree, like it is expressed in
152 perllexwarn. Also see "BUGS AND LIMITATIONS".
153
154 Thanks to the grouping in a tree, it's possible to test simply for
155 an 'io' warning, instead of testing for a
156 'closed|exec|layer|newline|pipe|unopened' warning.
157
158 Note, that warnings occurring at compile time can only be caught in
159 an eval block. So
160
161 warning_like {eval q/"$x"; $x;/}
162 [qw/void uninitialized/],
163 "some warnings at compile time";
164
165 will work, while it wouldn't work without the eval.
166
167 Note, that it isn't possible yet, to test for own categories,
168 created with warnings::register.
169
170 warnings_like BLOCK ARRAYREF, TEST_NAME
171 Tests to see that BLOCK gives exactly the number of the specified
172 warnings, in the defined order.
173
174 Please read also the notes to warning_like() as these methods are
175 only aliases.
176
177 Similar to warnings_are(), you can test for multiple warnings via
178 "carp" and for warning categories, too:
179
180 warnings_like {foo()}
181 [qr/bar warning/,
182 qr/bar warning/,
183 {carped => qr/bar warning/i},
184 'io'
185 ],
186 "I hope you'll never have to write a test for so many warnings :-)";
187
188 warnings_exist BLOCK STRING|ARRAYREF, TEST_NAME
189 Same as warning_like, but will warn() all warnings that do not
190 match the supplied regex/category, instead of registering an error.
191 Use this test when you just want to make sure that specific
192 warnings were generated, and couldn't care less if other warnings
193 happened in the same block of code.
194
195 warnings_exist {...} [qr/expected warning/], "Expected warning is thrown";
196
197 warnings_exist {...} ['uninitialized'], "Expected warning is thrown";
198
199 EXPORT
200 "warning_is", "warnings_are", "warning_like", "warnings_like",
201 "warnings_exist" by default.
202
204 Category check is done as "qr/category_name/". In some case this works,
205 like for category 'uninitialized'. For 'utf8' it does not work. Perl
206 does not have a list of warnings, so it is not possible to generate one
207 for "Test::Warn".
208
209 If you want to add a warning to a category, send a pull request.
210 Modifications should be done to %warnings_in_category. You should look
211 into perl source to check how warning is looking exactly.
212
213 Please note that warnings with newlines inside are very awkward. The
214 only sensible way to handle them is to use the "warning_like" or
215 "warnings_like" methods. The background is that there is no really safe
216 way to distinguish between warnings with newlines and a stacktrace.
217
218 If a method has its own warn handler, overwriting $SIG{__WARN__}, my
219 test warning methods won't get these warnings.
220
221 The "warning_like BLOCK CATEGORY, TEST_NAME" method isn't fully tested.
222 Please take note if you use this this calling style, and report any
223 bugs you find.
224
225 XS warnings
226 As described in
227 https://rt.cpan.org/Ticket/Display.html?id=42070&results=3c71d1b101a730e185691657f3b02f21
228 or https://github.com/hanfried/test-warn/issues/1 XS warnings might not
229 be caught.
230
232 Have a look to the similar Test::Exception module. Test::Trap
233
235 Many thanks to Adrian Howard, chromatic and Michael G. Schwern, who
236 have given me a lot of ideas.
237
239 Janek Schleicher, <bigj AT kamelfreund.de>
240
242 Copyright 2002 by Janek Schleicher
243
244 Copyright 2007-2014 by Alexandr Ciornii, <http://chorny.net/>
245
246 Copyright 2015-2018 by Janek Schleicher
247
248 This library is free software; you can redistribute it and/or modify it
249 under the same terms as Perl itself.
250
251
252
253perl v5.38.0 2023-07-21 Warn(3)