Test::Fatal - incredibly simple helpers for testing code with exceptions
version 0.004
use Test::More; use Test::Fatal; use System::Under::Test qw(might_die); is( exception { might_die; }, undef "the code lived", ); isnt( exception { might_die; }, undef, "the code died", ); isa_ok( exception { might_die; }, 'Exception::Whatever', 'the thrown exception', );
Test::Fatal is an alternative to the popular Test::Exception. It does much less, but should allow greater flexibility in testing exception-throwing code with about the same amount of typing.
It exports one routine by default: exception.
exception
my $exception = exception { ... };
exception takes a bare block of code and returns the exception thrown by that block. If no exception was thrown, it returns undef.
ACHTUNG! If the block results in a false exception, such as 0 or the empty string, Test::Fatal itself will die. Since either of these cases indicates a serious problem with the system under testing, this behavior is considered a feature. If you must test for these conditions, you should use Try::Tiny's try/catch mechanism. (Try::Tiny is the underlying exception handling system of Test::Fatal.)
Note that there is no TAP assert being performed. In other words, no "ok" or "not ok" line is emitted. It's up to you to use the rest of exception in an existing test like ok, isa_ok, is, et cetera.
ok
isa_ok
is
exception does not alter the stack presented to the called block, meaning that if the exception returned has a stack trace, it will include some frames between the code calling exception and the thing throwing the exception. This is considered a feature because it avoids the occasionally twitchy Sub::Uplevel mechanism.
Sub::Uplevel
try { should_live; } catch { fail("boo, we died"); } success { pass("hooray, we lived"); };
success, exported only by request, is a Try::Tiny helper with semantics identical to finally, but the body of the block will only be run if the try block ran without error.
success
finally
try
Although almost any needed exception tests can be performed with exception, success blocks may sometimes help organize complex testing.
Ricardo Signes <rjbs@cpan.org>
This software is copyright (c) 2010 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Test::Fatal, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Fatal
CPAN shell
perl -MCPAN -e shell install Test::Fatal
For more information on module installation, please visit the detailed CPAN module installation guide.