How to test for exceptions with Test::Exception in Perl?
Question
How to test for exceptions with Test::Exception in Perl?
Testing exceptions is an important part of writing robust Perl code. The Test::Exception module provides convenient functions to test whether your code throws exceptions (dies or croaks) as expected. It integrates smoothly with Test::More, which is commonly used for writing test scripts.
Key functions in Test::Exception
throws_ok { ... } $regexp, $test_name: Verify the code block throws an exception matching the regex.lives_ok { ... } $test_name: Verify the code block does NOT throw an exception.dies_ok { ... } $test_name: Test if the code block throws any exception.
Each function takes a bare code block and checks whether it throws an exception. This lets you isolate fault cases and ensure expected error handling behavior.
Perl Concepts in Exception Testing
In Perl, exceptions are usually thrown with die. The throws_ok and related functions catch exceptions internally using eval. When testing, you want fine-grained control over exception patterns, so Test::Exception's regex matching is ideal.
Version Notes
Test::Exceptionis widely available and works well with Perl 5.10+ (and earlier versions).- Its syntax has been consistent, so no significant version differences affect the usage.
Common Pitfalls
- Remember to pass a block to the functions (not a string). For example,
throws_ok { foo() }notthrows_ok("foo()"). - The regex should match the exception message exactly. Use
qr//or strings accordingly. - The test script must
use Test::More tests => Nwith the correct number of planned tests.
Example: Testing Exception with Test::Exception
use strict;
use warnings;
use Test::More tests => 3;
use Test::Exception;
# A simple function which dies if negative input is given
sub sqrt_positive {
my ($n) = @_;
die "Negative input: $n" if $n < 0;
return sqrt($n);
}
# Test 1. Expect an exception when input is negative
throws_ok { sqrt_positive(-4) }
qr/Negative input: -4/,
'Throws exception for negative input';
# Test 2. Expect no exception when input is positive
lives_ok { sqrt_positive(9) }
'Does not throw for positive input';
# Test 3. Confirm the function returns expected value
is(sqrt_positive(16), 4, 'Square root computed correctly');
This script:
- Uses
throws_okto confirm the negative input triggers an exception with the right message. - Uses
lives_okto verify no exception on valid input. - Uses
isfromTest::Moreto check the correct return value.
Run the script with perl yourtest.t or via perl - by copy-pasting it, and you’ll see clear pass/fail results.
Summary
Test::Exception is a simple yet powerful way to verify your Perl code throws (or does not throw) exceptions as expected. Use throws_ok and lives_ok with properly formatted regex and code blocks to write expressive tests that improve your code quality and reliability.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 62ms
1..3
ok 1 - Throws exception for negative input
ok 2 - Does not throw for positive input
ok 3 - Square root computed correctly
(empty)Was this helpful?
Related Questions
- How to use Test::Deep for complex data structure comparison?
- How to test Mojolicious applications in Perl?
- How to test HTTP responses with Test::WWW::Mechanize in Perl?
- How to use prove command to run Perl tests?
- How to test database code with Test::mysqld in Perl?
- How to use Test::Fatal for exception testing in Perl?