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 warning to calc 2+2"; # or
15 warnings_are {add(2,2)} [], "No warning to calc 2+2"; # what 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 found 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
31 This module provides a few convenience methods for testing warning
32 based code.
33
34 If you are not already familiar with the Test::More manpage now would
35 be the time to go take a look.
36
37 FUNCTIONS
38
39 warning_is BLOCK STRING, TEST_NAME
40 Tests that BLOCK gives exactly the one specificated warning. The
41 test fails if the BLOCK warns more then one times or doesn't warn.
42 If the string is undef, then the tests succeeds iff the BLOCK
43 doesn't give any warning. Another way to say that there aren't ary
44 warnings in the block, is "warnings_are {foo()} [], "no warnings
45 in"".
46
47 If you want to test for a warning given by carp, You have to write
48 something like: "warning_is {carp "msg"} {carped => 'msg'}, "Test
49 for a carped warning"". The test will fail, if a "normal" warning
50 is found instead of a "carped" one.
51
52 Note: "warn "foo"" would print something like "foo at -e line 1".
53 This method ignores everything after the at. That means, to match
54 this warning you would have to call "warning_is {warn "foo"} "foo",
55 "Foo succeeded"". If you need to test for a warning at an exactly
56 line, try better something like "warning_like {warn "foo"} qr/at
57 XYZ.dat line 5/".
58
59 warning_is and warning_are are only aliases to the same method. So
60 you also could write "warning_is {foo()} [], "no warning"" or some‐
61 thing similar. I decided me to give two methods to have some bet‐
62 ter readable method names.
63
64 A true value is returned if the test succeeds, false otherwise.
65
66 The test name is optional, but recommended.
67
68 warnings_are BLOCK ARRAYREF, TEST_NAME
69 Tests to see that BLOCK gives exactly the specificated warnings.
70 The test fails if the BLOCK warns a different number than the size
71 of the ARRAYREf would have expected. If the ARRAYREF is equal to
72 [], then the test succeeds iff the BLOCK doesn't give any warning.
73
74 Please read also the notes to warning_is as these methods are only
75 aliases.
76
77 If you want more than one tests for carped warnings look that way:
78 "warnings_are {carp "c1"; carp "c2"} {carped =" ['c1','c2'];> or
79 "warnings_are {foo()} ["Warning 1", {carped =" ["Carp 1", "Carp
80 2"]}, "Warning 2"]>. Note that "{carped =" ...}> has always to be
81 a hash ref.
82
83 warning_like BLOCK REGEXP, TEST_NAME
84 Tests that BLOCK gives exactly one warning and it can be matched to
85 the given regexp. If the string is undef, then the tests succeeds
86 iff the BLOCK doesn't give any warning.
87
88 The REGEXP is matched after the whole warn line, which consists in
89 general of "WARNING at __FILE__ line __LINE__". So you can check
90 for a warning in at File Foo.pm line 5 with "warning_like {bar()}
91 qr/at Foo.pm line 5/, "Testname"". I don't know whether it's sens‐
92 ful to do such a test :-( However, you should be prepared as a
93 matching with 'at', 'file', '\d' or similar will always pass.
94 Think to the qr/^foo/ if you want to test for warning "foo some‐
95 thing" in file foo.pl.
96
97 You can also write the regexp in a string as "/.../" instead of
98 using the qr/.../ syntax. Note that the slashes are important in
99 the string, as strings without slashes are reserved for warning
100 categories (to match warning categories as can be seen in the per‐
101 llexwarn man page).
102
103 Similar to "warning_is", you can test for warnings via "carp" with:
104 "warning_like {bar()} {carped =" qr/bar called too early/i};>
105
106 Similar to "warning_is"/"warnings_are", "warning_like" and "warn‐
107 ings_like" are only aliases to the same methods.
108
109 A true value is returned if the test succeeds, false otherwise.
110
111 The test name is optional, but recommended.
112
113 warning_like BLOCK STRING, TEST_NAME
114 Tests whether a BLOCK gives exactly one warning of the passed cate‐
115 gory. The categories are grouped in a tree, like it is expressed
116 in perllexwarn. Note, that they have the hierarchical structure
117 from perl 5.8.0, wich has a little bit changed to 5.6.1 or earlier
118 versions (You can access the internal used tree with
119 $Test::Warn::Categorization::tree, allthough I wouldn't recommend
120 it)
121
122 Thanks to the grouping in a tree, it's simple possible to test for
123 an 'io' warning, instead for testing for a 'closed⎪exec⎪layer⎪new‐
124 line⎪pipe⎪unopened' warning.
125
126 Note, that warnings occuring at compile time, can only be catched
127 in an eval block. So
128
129 warning_like {eval q/"$x"; $x;/}
130 [qw/void uninitialized/],
131 "some warnings at compile time";
132
133 will work, while it wouldn't work without the eval.
134
135 Note, that it isn't possible yet, to test for own categories, cre‐
136 ated with warnings::register.
137
138 warnings_like BLOCK ARRAYREF, TEST_NAME
139 Tests to see that BLOCK gives exactly the number of the specifi‐
140 cated warnings and all the warnings have to match in the defined
141 order to the passed regexes.
142
143 Please read also the notes to warning_like as these methods are
144 only aliases.
145
146 Similar to "warnings_are", you can test for multiple warnings via
147 "carp" and for warning categories, too:
148
149 warnings_like {foo()}
150 [qr/bar warning/,
151 qr/bar warning/,
152 {carped => qr/bar warning/i},
153 'io'
154 ],
155 "I hope, you'll never have to write a test for so many warnings :-)";
156
157 EXPORT
158
159 "warning_is", "warnings_are", "warning_like", "warnings_like" by
160 default.
161
163 Please note that warnings with newlines inside are making a lot of
164 trouble. The only sensful way to handle them is to use are the "warn‐
165 ing_like" or "warnings_like" methods. Background for these problems is
166 that there is no really secure way to distinguish between warnings with
167 newlines and a tracing stacktrace.
168
169 If a method has it's own warn handler, overwriting $SIG{__WARN__}, my
170 test warning methods won't get these warnings.
171
172 The "warning_like BLOCK CATEGORY, TEST_NAME" method isn't extremely
173 tested. Please use this calling style with higher attention and tell
174 me if you find a bug.
175
177 Improve this documentation.
178
179 The code has some parts doubled - especially in the test scripts. This
180 is really awkward and has to be changed.
181
182 Please feel free to suggest me any improvements.
183
185 Have a look to the similar Test::Exception module.
186
188 Many thanks to Adrian Howard, chromatic and Michael G. Schwern, who
189 have given me a lot of ideas.
190
192 Janek Schleicher, <bigj AT kamelfreund.de>
193
195 Copyright 2002 by Janek Schleicher
196
197 This library is free software; you can redistribute it and/or modify it
198 under the same terms as Perl itself.
199
200
201
202perl v5.8.8 2007-04-30 Warn(3)