1Test::Exception(3) User Contributed Perl Documentation Test::Exception(3)
2
3
4
6 Test::Exception - Test exception based code
7
9 use Test::More tests => 5;
10 use Test::Exception;
11
12 # or if you don't need Test::More
13
14 use Test::Exception tests => 5;
15
16 # then...
17
18 # Check that the stringified exception matches given regex
19 throws_ok { $foo->method } qr/division by zero/, 'zero caught okay';
20
21 # Check an exception of the given class (or subclass) is thrown
22 throws_ok { $foo->method } 'Error::Simple', 'simple error thrown';
23
24 # all Test::Exceptions subroutines are guaranteed to preserve the state
25 # of $@ so you can do things like this after throws_ok and dies_ok
26 like $@, 'what the stringified exception should look like';
27
28 # Check that something died - we do not care why
29 dies_ok { $foo->method } 'expecting to die';
30
31 # Check that something did not die
32 lives_ok { $foo->method } 'expecting to live';
33
34 # Check that a test runs without an exception
35 lives_and { is $foo->method, 42 } 'method is 42';
36
37 # or if you don't like prototyped functions
38
39 throws_ok( sub { $foo->method }, qr/division by zero/,
40 'zero caught okay' );
41 throws_ok( sub { $foo->method }, 'Error::Simple',
42 'simple error thrown' );
43 dies_ok( sub { $foo->method }, 'expecting to die' );
44 lives_ok( sub { $foo->method }, 'expecting to live' );
45 lives_and( sub { is $foo->method, 42 }, 'method is 42' );
46
48 This module provides a few convenience methods for testing exception
49 based code. It is built with Test::Builder and plays happily with
50 Test::More and friends.
51
52 If you are not already familiar with Test::More now would be the time
53 to go take a look.
54
55 You can specify the test plan when you "use Test::Exception" in the
56 same way as "use Test::More". See Test::More for details.
57
58 NOTE: Test::Exception only checks for exceptions. It will ignore other
59 methods of stopping program execution - including exit(). If you have
60 an exit() in evalled code Test::Exception will not catch this with any
61 of its testing functions.
62
63 throws_ok
64 Tests to see that a specific exception is thrown. throws_ok() has
65 two forms:
66
67 throws_ok BLOCK REGEX, TEST_DESCRIPTION
68 throws_ok BLOCK CLASS, TEST_DESCRIPTION
69
70 In the first form the test passes if the stringified exception
71 matches the give regular expression. For example:
72
73 throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file';
74
75 If your perl does not support "qr//" you can also pass a regex-like
76 string, for example:
77
78 throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file';
79
80 The second form of throws_ok() test passes if the exception is of
81 the same class as the one supplied, or a subclass of that class.
82 For example:
83
84 throws_ok { $foo->bar } "Error::Simple", 'simple error';
85
86 Will only pass if the "bar" method throws an Error::Simple
87 exception, or a subclass of an Error::Simple exception.
88
89 You can get the same effect by passing an instance of the exception
90 you want to look for. The following is equivalent to the previous
91 example:
92
93 my $SIMPLE = Error::Simple->new;
94 throws_ok { $foo->bar } $SIMPLE, 'simple error';
95
96 Should a throws_ok() test fail it produces appropriate diagnostic
97 messages. For example:
98
99 not ok 3 - simple error
100 # Failed test (test.t at line 48)
101 # expecting: Error::Simple exception
102 # found: normal exit
103
104 Like all other Test::Exception functions you can avoid prototypes
105 by passing a subroutine explicitly:
106
107 throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' );
108
109 A true value is returned if the test succeeds, false otherwise. On
110 exit $@ is guaranteed to be the cause of death (if any).
111
112 A description of the exception being checked is used if no optional
113 test description is passed.
114
115 NOTE: Rememeber when you "die $string_without_a_trailing_newline"
116 perl will automatically add the current script line number, input
117 line number and a newline. This will form part of the string that
118 throws_ok regular expressions match against.
119
120 dies_ok
121 Checks that a piece of code dies, rather than returning normally.
122 For example:
123
124 sub div {
125 my ( $a, $b ) = @_;
126 return $a / $b;
127 };
128
129 dies_ok { div( 1, 0 ) } 'divide by zero detected';
130
131 # or if you don't like prototypes
132 dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' );
133
134 A true value is returned if the test succeeds, false otherwise. On
135 exit $@ is guaranteed to be the cause of death (if any).
136
137 Remember: This test will pass if the code dies for any reason. If
138 you care about the reason it might be more sensible to write a more
139 specific test using throws_ok().
140
141 The test description is optional, but recommended.
142
143 lives_ok
144 Checks that a piece of code doesn't die. This allows your test
145 script to continue, rather than aborting if you get an unexpected
146 exception. For example:
147
148 sub read_file {
149 my $file = shift;
150 local $/;
151 open my $fh, '<', $file or die "open failed ($!)\n";
152 $file = <FILE>;
153 return $file;
154 };
155
156 my $file;
157 lives_ok { $file = read_file('test.txt') } 'file read';
158
159 # or if you don't like prototypes
160 lives_ok( sub { $file = read_file('test.txt') }, 'file read' );
161
162 Should a lives_ok() test fail it produces appropriate diagnostic
163 messages. For example:
164
165 not ok 1 - file read
166 # Failed test (test.t at line 15)
167 # died: open failed (No such file or directory)
168
169 A true value is returned if the test succeeds, false otherwise. On
170 exit $@ is guaranteed to be the cause of death (if any).
171
172 The test description is optional, but recommended.
173
174 lives_and
175 Run a test that may throw an exception. For example, instead of
176 doing:
177
178 my $file;
179 lives_ok { $file = read_file('answer.txt') } 'read_file worked';
180 is $file, "42", 'answer was 42';
181
182 You can use lives_and() like this:
183
184 lives_and { is read_file('answer.txt'), "42" } 'answer is 42';
185 # or if you don't like prototypes
186 lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42');
187
188 Which is the same as doing
189
190 is read_file('answer.txt'), "42\n", 'answer is 42';
191
192 unless "read_file('answer.txt')" dies, in which case you get the
193 same kind of error as lives_ok()
194
195 not ok 1 - answer is 42
196 # Failed test (test.t at line 15)
197 # died: open failed (No such file or directory)
198
199 A true value is returned if the test succeeds, false otherwise. On
200 exit $@ is guaranteed to be the cause of death (if any).
201
202 The test description is optional, but recommended.
203
205 Sometimes we want to use Test::Exception tests in a test suite, but
206 don't want to force the user to have Test::Exception installed. One way
207 to do this is to skip the tests if Test::Exception is absent. You can
208 do this with code something like this:
209
210 use strict;
211 use warnings;
212 use Test::More;
213
214 BEGIN {
215 eval "use Test::Exception";
216 plan skip_all => "Test::Exception needed" if $@;
217 }
218
219 plan tests => 2;
220 # ... tests that need Test::Exception ...
221
222 Note that we load Test::Exception in a "BEGIN" block ensuring that the
223 subroutine prototypes are in place before the rest of the test script
224 is compiled.
225
227 There are some edge cases in Perl's exception handling where
228 Test::Exception will miss exceptions thrown in DESTROY blocks. See the
229 RT bug <http://rt.cpan.org/Ticket/Display.html?id=24678> for details,
230 along with the t/edge-cases.t in the distribution test suite. These
231 will be addressed in a future Test::Exception release.
232
233 If you find any more bugs please let me know by e-mail, or report the
234 problem with <http://rt.cpan.org/>.
235
237 perl-qa
238 If you are interested in testing using Perl I recommend you visit
239 <http://qa.perl.org/> and join the excellent perl-qa mailing list.
240 See <http://lists.perl.org/showlist.cgi?name=perl-qa> for details
241 on how to subscribe.
242
243 perlmonks
244 You can find users of Test::Exception, including the module author,
245 on <http://www.perlmonks.org/>. Feel free to ask questions on
246 Test::Exception there.
247
248 CPAN::Forum
249 The CPAN Forum is a web forum for discussing Perl's CPAN modules.
250 The Test::Exception forum can be found at
251 <http://www.cpanforum.com/dist/Test-Exception>.
252
253 AnnoCPAN
254 AnnoCPAN is a web site that allows community annotations of Perl
255 module documentation. The Test::Exception annotations can be found
256 at <http://annocpan.org/~ADIE/Test-Exception/>.
257
259 If you think this module should do something that it doesn't (or does
260 something that it shouldn't) please let me know.
261
262 You can see my current to do list at
263 <http://adrianh.tadalist.com/lists/public/15421>, with an RSS feed of
264 changes at <http://adrianh.tadalist.com/lists/feed_public/15421>.
265
267 Thanks to chromatic and Michael G Schwern for the excellent
268 Test::Builder, without which this module wouldn't be possible.
269
270 Thanks to Adam Kennedy, Andy Lester, Aristotle Pagaltzis, Ben Prew,
271 Cees Hek, Chris Dolan, chromatic, Curt Sampson, David Cantrell, David
272 Golden, David Tulloh, David Wheeler, J. K. O'Brien, Janek Schleicher,
273 Jim Keenan, Jos I. Boumans, Joshua ben Jore, Jost Krieger, Mark Fowler,
274 Michael G Schwern, Nadim Khemir, Paul McCann, Perrin Harkins, Peter
275 Rabbitson, Peter Scott, Ricardo Signes, Rob Muhlestein, Scott R. Godin,
276 Steve Purkis, Steve, Tim Bunce, and various anonymous folk for
277 comments, suggestions, bug reports and patches.
278
280 Adrian Howard <adrianh@quietstars.com>
281
282 If you can spare the time, please drop me a line if you find this
283 module useful.
284
286 <http://del.icio.us/tag/Test::Exception>
287 Delicious links on Test::Exception.
288
289 Test::Warn & Test::NoWarnings
290 Modules to help test warnings.
291
292 Test::Builder
293 Support module for building test libraries.
294
295 Test::Simple & Test::More
296 Basic utilities for writing tests.
297
298 <http://qa.perl.org/test-modules.html>
299 Overview of some of the many testing modules available on CPAN.
300
301 <http://del.icio.us/tag/perl+testing>
302 Delicious links on perl testing.
303
305 Copyright 2002-2007 Adrian Howard, All Rights Reserved.
306
307 This program is free software; you can redistribute it and/or modify it
308 under the same terms as Perl itself.
309
310
311
312perl v5.16.3 2014-06-10 Test::Exception(3)