1Test::Fatal(3) User Contributed Perl Documentation Test::Fatal(3)
2
3
4
6 Test::Fatal - incredibly simple helpers for testing code with
7 exceptions
8
10 version 0.003
11
13 use Test::More;
14 use Test::Fatal;
15
16 use System::Under::Test qw(might_die);
17
18 is(
19 exception { might_die; },
20 undef
21 "the code lived",
22 );
23
24 isnt(
25 exception { might_die; },
26 undef,
27 "the code died",
28 );
29
30 isa_ok(
31 exception { might_die; },
32 'Exception::Whatever',
33 'the thrown exception',
34 );
35
37 Test::Fatal is an alternative to the popular Test::Exception. It does
38 much less, but should allow greater flexibility in testing exception-
39 throwing code with about the same amount of typing.
40
41 It exports one routine by default: "exception".
42
44 exception
45 my $exception = exception { ... };
46
47 "exception" takes a bare block of code and returns the exception thrown
48 by that block. If no exception was thrown, it returns undef.
49
50 ACHTUNG! If the block results in a false exception, such as 0 or the
51 empty string, Test::Fatal itself will die. Since either of these cases
52 indicates a serious problem with the system under testing, this
53 behavior is considered a feature. If you must test for these
54 conditions, you should use Try::Tiny's try/catch mechanism. (Try::Tiny
55 is the underlying exception handling system of Test::Fatal.)
56
57 Note that there is no TAP assert being performed. In other words, no
58 "ok" or "not ok" line is emitted. It's up to you to use the rest of
59 "exception" in an existing test like "ok", "isa_ok", "is", et cetera.
60
61 "exception" does not alter the stack presented to the called block,
62 meaning that if the exception returned has a stack trace, it will
63 include some frames between the code calling "exception" and the thing
64 throwing the exception. This is considered a feature because it avoids
65 the occasionally twitchy "Sub::Uplevel" mechanism.
66
67 success
68 try {
69 should_live;
70 } catch {
71 fail("boo, we died");
72 } success {
73 pass("hooray, we lived");
74 };
75
76 "success", exported only by request, is a Try::Tiny helper with
77 semantics identical to "finally", but the body of the block will only
78 be run if the "try" block ran without error.
79
80 Although almost any needed exception tests can be performed with
81 "exception", success blocks may sometimes help organize complex
82 testing.
83
85 Ricardo Signes <rjbs@cpan.org>
86
88 This software is copyright (c) 2010 by Ricardo Signes.
89
90 This is free software; you can redistribute it and/or modify it under
91 the same terms as the Perl 5 programming language system itself.
92
93
94
95perl v5.12.2 2010-11-26 Test::Fatal(3)