I recommend Test::Fatal rather than Test::Exception.
Test::Exception has been around for a long time, so a lot of existing test suites use it, but Test::Fatal is easier to learn. Test::Fatal exports only 1 function: exception
. This runs the associated code and returns the exception that it threw, or undef
if it ran without error. You then test that return value using any of the normal Test::More functions like is
, isnt
, like
, or isa_ok
.
Test::Exception requires you to learn its own testing functions like throws_ok
and dies_ok
, and remember that you don't put a comma between the code and the test name.
So, your example would be:
use Test::More;
use Test::Fatal;
my $obj = ...;
isnt(exception { $obj->method($my, $bad, $params) },
undef, 'method dies as expected');
Or you could use like
to match the expected error message, or isa_ok
to test if it threw the correct class of exception object.
Test::Fatal just gives you more flexibility with less learning curve than Test::Exception.