I find myself often including something like the following in my tests when ensuring that parameters are being validated properly, or that a subroutine is failing by throwing an exception when I expect it to:
use Test::More tests => 1; # Just for example... use Try::Tiny; my $caught; try { # Call to subroutine being tested. } catch { $caught = $_ }; ok( defined( $caught ), "tested_sub(): Threw an exception given XYZ params" ); like( $caught, qr/exception keyphrase/, "tested_sub(): Proper exception reported for xyz." );
I'm sure there is more than one way to do it, but I'm looking to see if others have a better approach. This works fine from what I can tell; at least my tests work as I expect them to. But is there already a clearer and more concise solution I should be looking at?
Dave
In reply to Testing for exceptions with Test::More by davido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |