1Test::Exception(3)    User Contributed Perl Documentation   Test::Exception(3)
2
3
4

NAME

6       Test::Exception - Test exception-based code
7

SYNOPSIS

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

DESCRIPTION

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       NOTE: This module uses Sub::Uplevel and relies on overriding
64       "CORE::GLOBAL::caller" to hide your test blocks from the call stack.
65       If this use of global overrides concerns you, the Test::Fatal module
66       offers a more minimalist alternative.
67
68       throws_ok
69           Tests to see that a specific exception is thrown. throws_ok() has
70           two forms:
71
72             throws_ok BLOCK REGEX, TEST_DESCRIPTION
73             throws_ok BLOCK CLASS, TEST_DESCRIPTION
74
75           In the first form the test passes if the stringified exception
76           matches the give regular expression. For example:
77
78               throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file';
79
80           If your perl does not support "qr//" you can also pass a regex-like
81           string, for example:
82
83               throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file';
84
85           The second form of throws_ok() test passes if the exception is of
86           the same class as the one supplied, or a subclass of that class.
87           For example:
88
89               throws_ok { $foo->bar } "Error::Simple", 'simple error';
90
91           Will only pass if the "bar" method throws an Error::Simple
92           exception, or a subclass of an Error::Simple exception.
93
94           You can get the same effect by passing an instance of the exception
95           you want to look for. The following is equivalent to the previous
96           example:
97
98               my $SIMPLE = Error::Simple->new;
99               throws_ok { $foo->bar } $SIMPLE, 'simple error';
100
101           Should a throws_ok() test fail it produces appropriate diagnostic
102           messages. For example:
103
104               not ok 3 - simple error
105               #     Failed test (test.t at line 48)
106               # expecting: Error::Simple exception
107               # found: normal exit
108
109           Like all other Test::Exception functions you can avoid prototypes
110           by passing a subroutine explicitly:
111
112               throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' );
113
114           A true value is returned if the test succeeds, false otherwise. On
115           exit $@ is guaranteed to be the cause of death (if any).
116
117           A description of the exception being checked is used if no optional
118           test description is passed.
119
120           NOTE: Remember when you "die $string_without_a_trailing_newline"
121           perl will automatically add the current script line number, input
122           line number and a newline. This will form part of the string that
123           throws_ok regular expressions match against.
124
125       dies_ok
126           Checks that a piece of code dies, rather than returning normally.
127           For example:
128
129               sub div {
130                   my ( $a, $b ) = @_;
131                   return $a / $b;
132               };
133
134               dies_ok { div( 1, 0 ) } 'divide by zero detected';
135
136               # or if you don't like prototypes
137               dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' );
138
139           A true value is returned if the test succeeds, false otherwise. On
140           exit $@ is guaranteed to be the cause of death (if any).
141
142           Remember: This test will pass if the code dies for any reason. If
143           you care about the reason it might be more sensible to write a more
144           specific test using throws_ok().
145
146           The test description is optional, but recommended.
147
148       lives_ok
149           Checks that a piece of code doesn't die. This allows your test
150           script to continue, rather than aborting if you get an unexpected
151           exception. For example:
152
153               sub read_file {
154                   my $file = shift;
155                   local $/;
156                   open my $fh, '<', $file or die "open failed ($!)\n";
157                   $file = <FILE>;
158                   return $file;
159               };
160
161               my $file;
162               lives_ok { $file = read_file('test.txt') } 'file read';
163
164               # or if you don't like prototypes
165               lives_ok( sub { $file = read_file('test.txt') }, 'file read' );
166
167           Should a lives_ok() test fail it produces appropriate diagnostic
168           messages. For example:
169
170               not ok 1 - file read
171               #     Failed test (test.t at line 15)
172               # died: open failed (No such file or directory)
173
174           A true value is returned if the test succeeds, false otherwise. On
175           exit $@ is guaranteed to be the cause of death (if any).
176
177           The test description is optional, but recommended.
178
179       lives_and
180           Run a test that may throw an exception. For example, instead of
181           doing:
182
183             my $file;
184             lives_ok { $file = read_file('answer.txt') } 'read_file worked';
185             is $file, "42", 'answer was 42';
186
187           You can use lives_and() like this:
188
189             lives_and { is read_file('answer.txt'), "42" } 'answer is 42';
190             # or if you don't like prototypes
191             lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42');
192
193           Which is the same as doing
194
195             is read_file('answer.txt'), "42\n", 'answer is 42';
196
197           unless "read_file('answer.txt')" dies, in which case you get the
198           same kind of error as lives_ok()
199
200             not ok 1 - answer is 42
201             #     Failed test (test.t at line 15)
202             # died: open failed (No such file or directory)
203
204           A true value is returned if the test succeeds, false otherwise. On
205           exit $@ is guaranteed to be the cause of death (if any).
206
207           The test description is optional, but recommended.
208

SKIPPING TEST::EXCEPTION TESTS

210       Sometimes we want to use Test::Exception tests in a test suite, but
211       don't want to force the user to have Test::Exception installed. One way
212       to do this is to skip the tests if Test::Exception is absent. You can
213       do this with code something like this:
214
215         use strict;
216         use warnings;
217         use Test::More;
218
219         BEGIN {
220             eval "use Test::Exception";
221             plan skip_all => "Test::Exception needed" if $@;
222         }
223
224         plan tests => 2;
225         # ... tests that need Test::Exception ...
226
227       Note that we load Test::Exception in a "BEGIN" block ensuring that the
228       subroutine prototypes are in place before the rest of the test script
229       is compiled.
230

BUGS

232       There are some edge cases in Perl's exception handling where
233       Test::Exception will miss exceptions thrown in DESTROY blocks. See the
234       RT bug <http://rt.cpan.org/Ticket/Display.html?id=24678> for details,
235       along with the t/edge-cases.t in the distribution test suite. These
236       will be addressed in a future Test::Exception release.
237
238       If you find any more bugs please let me know by e-mail, or report the
239       problem with <http://rt.cpan.org/>.
240

COMMUNITY

242       perl-qa
243           If you are interested in testing using Perl I recommend you visit
244           <http://qa.perl.org/> and join the excellent perl-qa mailing list.
245           See <http://lists.perl.org/showlist.cgi?name=perl-qa> for details
246           on how to subscribe.
247
248       perlmonks
249           You can find users of Test::Exception, including the module author,
250           on  <http://www.perlmonks.org/>. Feel free to ask questions on
251           Test::Exception there.
252
253       CPAN::Forum
254           The CPAN Forum is a web forum for discussing Perl's CPAN modules.
255           The Test::Exception forum can be found at
256           <http://www.cpanforum.com/dist/Test-Exception>.
257
258       AnnoCPAN
259           AnnoCPAN is a web site that allows community annotations of Perl
260           module documentation. The Test::Exception annotations can be found
261           at <http://annocpan.org/~ADIE/Test-Exception/>.
262

TO DO

264       If you think this module should do something that it doesn't (or does
265       something that it shouldn't) please let me know.
266
267       You can see my current to do list at
268       <http://adrianh.tadalist.com/lists/public/15421>, with an RSS feed of
269       changes at <http://adrianh.tadalist.com/lists/feed_public/15421>.
270

ACKNOWLEDGMENTS

272       Thanks to chromatic and Michael G Schwern for the excellent
273       Test::Builder, without which this module wouldn't be possible.
274
275       Thanks to Adam Kennedy, Andy Lester, Aristotle Pagaltzis, Ben Prew,
276       Cees Hek, Chris Dolan, chromatic, Curt Sampson, David Cantrell, David
277       Golden, David Tulloh, David Wheeler, J. K. O'Brien, Janek Schleicher,
278       Jim Keenan, Jos I. Boumans, Joshua ben Jore, Jost Krieger, Mark Fowler,
279       Michael G Schwern, Nadim Khemir, Paul McCann, Perrin Harkins, Peter
280       Rabbitson, Peter Scott, Ricardo Signes, Rob Muhlestein, Scott R. Godin,
281       Steve Purkis, Steve, Tim Bunce, and various anonymous folk for
282       comments, suggestions, bug reports and patches.
283

AUTHOR

285       Adrian Howard <adrianh@quietstars.com>
286
287       If you can spare the time, please drop me a line if you find this
288       module useful.
289

SEE ALSO

291       <http://del.icio.us/tag/Test::Exception>
292           Delicious links on Test::Exception.
293
294       Test::Fatal
295           A slightly different interface to testing exceptions, without
296           overriding "CORE::caller".
297
298       Test::Warnings & Test::Warn & Test::NoWarnings
299           Modules to help test warnings.
300
301       Test::Builder
302           Support module for building test libraries.
303
304       Test::Simple & Test::More
305           Basic utilities for writing tests.
306
307       <http://qa.perl.org/test-modules.html>
308           Overview of some of the many testing modules available on CPAN.
309
310       <http://del.icio.us/tag/perl+testing>
311           Delicious links on perl testing.
312

LICENCE

314       Copyright 2002-2007 Adrian Howard, All Rights Reserved.
315
316       This program is free software; you can redistribute it and/or modify it
317       under the same terms as Perl itself.
318
319
320
321perl v5.28.0                      2015-12-29                Test::Exception(3)
Impressum